sort
Sort the properties of a list.
list.sort([compareFunction])
Sorts the elements of a list in place and returns the list. The API is the
same as the native JavaScript Array.prototype.sort API.
import {DefineList} from "can";
const accounts = new DefineList([
{ name: "Savings", amount: 20.00 },
{ name: "Checking", amount: 103.24 },
{ name: "Kids Savings", amount: 48155.13 }
]);
accounts.sort((a, b) => {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name){
return 1;
} else {
return 0;
}
});
console.log(accounts[0].name); //-> "Checking"
console.log(accounts[1].name); //-> "Kids Savings"
console.log(accounts[2].name); //-> "Savings"
Parameters
- compareFunction
{function(a, b)}:Specifies a function that defines the sort order.
If
compareFunctionis supplied, the list elements are sorted according to the return value of the compare function. Ifaandbare two elements being compared, then:- If
compareFunction(a, b)returns a value less than 0,awill be sorted to a lower index thanb, soawill now come first. - If
compareFunction(a, b)returns 0, the order of the two values will not be changed. - If
compareFunction(a, b)returns a value greater than 0,awill be sorted to a higher index thanb, sobwill now come first.
- If