0

JavaScript program to sort the items of an array?

13th Apr 2017, 7:27 AM
Ajit Singh Kushwaha
Ajit Singh Kushwaha - avatar
2 Respostas
+ 23
array.sort(...) array.filter(...) array.map(...) .......
13th Apr 2017, 7:28 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 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
13th Apr 2017, 8:23 AM
Burey
Burey - avatar