have array [{id: 5}, {id: 1}, {id: 2}, {id: 0}]
i wanna create sorted another array [{id: 0}, {id: 1}, {id: 2}, {id: 5}]
. How i can do this with function filter
?
Asked
Active
Viewed 79 times
-4

Artur
- 1
- 1
-
1Why would you use `filter` for that?! – deceze Jul 11 '17 at 10:13
-
Why would `sort` and `new array` requires the use of `filter`? – ibrahim mahrir Jul 11 '17 at 10:13
-
Look at the [Array.prototype.sort](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function. – abhishekkannojia Jul 11 '17 at 10:13
-
Sort will do.. `array.sort( (a,b) => a.id > b.id )` – Rajesh Dan Jul 11 '17 at 10:14
-
1@RajeshDan `a.id - b.id` (negative and positive not true and false) :D – ibrahim mahrir Jul 11 '17 at 10:15
-
Yeah.. :D but again previous works for this scenario.. Thanks @ibrahimmahrir – Rajesh Dan Jul 11 '17 at 10:18
-
how sort date object – Artur Jul 11 '17 at 10:25
1 Answers
0
Use the sort method of the array to sort the values
array.sort(function(a, b) {
return a.id- b.id;
});

Joey Pinto
- 1,735
- 1
- 18
- 34