grouperArray.sort(function (a, b) { var aSize = a.gsize; var bSize = b.gsize; var aLow = a.glow; var bLow = b.glow; console.log(aLow + " | " + bLow); return (aSize < bSize) ? -1 : (aSize > bSize) ? 1 : 0; });
This code sorts the array by gsize
, smallest to largest.
How would I change it to sort first by gsize
and then by glow
?
Step 1 : Here we can take two pointers type0 (for element 0) starting from beginning (index = 0) and type1 (for element 1) starting from end index. Step 2: We intend to put 1 to the right side of the array. Once we have done this then 0 will definitely towards left side of array to achieve this we do following.
We can use . sort((a,b)=>a-b) to sort an array of numbers in ascending numerical order or . sort((a,b)=>b-a) for descending order.
grouperArray.sort(function (a, b) { return a.gsize - b.gsize || a.glow - b.glow; });
shorter version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With