I'm trying to sort an array
of objects
with ActionScript 3
.
The array is like this:
var arr:Array = new Array ();
arr.push ({name:"John", date:"20080324", message:"Hi"});
arr.push ({name:"Susan", date:"20090528", message:"hello"});
can I do something with Array.sort(...)
method?
Do what? If you're trying to e.g. sort by name, then date, use Array.sortOn
.
arr.sortOn(['name', 'date'])
Other than using sortOn
, which will work for fields like strings and numbers, if you have other objects or more complex logic, you could pass a compare function to the sort() method.
Your compare function will be called by the sort function as many times as neccesary to sort your array. Each time, it will pass to your function two of the arrays' objects. Here, you determine how these two objects sort and tell that to the sort function by returning:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With