'Sorting with Number and Strings in array of objects
I have array like this :
array =[{limit:50}, {limit:40}, {limit:10},{limit:'unlimited'}]
How to sort this array so that, when ascending unlimited comes at the last index and when descending unlimited comes at the top index.
Solution 1:[1]
Simplest and the most straightforward way is to use the Array built in functions of sort and reverse
In case you require ascending:
let array =[{limit:50}, {limit:40}, {limit:10},{limit:'unlimited'}]
array.sort((a,b) => a.limit - b.limit);
In case you require descending:
let array =[{limit:50}, {limit:40}, {limit:10},{limit:'unlimited'}]
array.sort((a,b) => a.limit - b.limit);
array.reverse();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ammar |
