order array js by a value 1

order array js by a value

arr.sort((a, b) => a.name > b.name ? 1 : -1);

Here is what the above code is Doing:
1. We’re creating a new array called sortedArr.
2. We’re using the sort() method on the array.
3. We’re passing in a callback function to the sort() method.
4. The callback function takes in two parameters, a and b.
5. The callback function returns 1 if a is greater than b.
6. The callback function returns -1 if a is less than b.
7. The sort() method will sort the array in ascending order.

Similar Posts