0
JavaScript program to sort the items of an array?
2 Respostas
+ 23
array.sort(...)
array.filter(...)
array.map(...)
.......
+ 5
to add to Valentin's answer, if you want to sort by id or some inner value of an object, you can pass a function to sort:
arr=[
{id:3,item:"apple"},
{id:2,item:"eggs"},
{id:4,item:"banana"},
{id:6,item:"kiwi"},
{id:5,item:"orange"},
{id:1,item:"spam"}
];
alert("Unsorted\n"+JSON.stringify(arr,null,2));
arr.sort(function(a, b) {
return a.id - b.id;
});
alert("Sorted\n"+JSON.stringify(arr,null,2));
just run this on web playground and you will see