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:
- a negative number: if the first
object comes before the second one
- a positive number: if the second
object comes before the first one
- 0: if both objects have the same sort
order.