How to sort correctly arrays in JavaScript?

Today we’re going to look at how to sort arrays (and numbered arrays) in JavaScript. Finally, we’ll see how to sort arrays containing objects by adding an attribute, such as priority or order, to display these objects in the desired order.

Array.p…


This content originally appeared on DEV Community and was authored by Pierre-Henry Soria ✨

Today we're going to look at how to sort arrays (and numbered arrays) in JavaScript. Finally, we'll see how to sort arrays containing objects by adding an attribute, such as priority or order, to display these objects in the desired order.

Array.prototype.sort() is a comparison function used to sort elements of an array.

The arguments of this function are:

  • compareFunction(a, b) < 0: a comes before b
  • compareFunction(a, b) > 0: b comes before a
  • compareFunction(a, b) = 0: the order of a and b is identical.

Examples (descending and ascending sorting):

const array = [1, 20, 8, 15, 25, 80, 100, 200];
// sort in descending order
array.sort((a, b) => b - a);
console.log(array);
const array = [1, 20, 8, 15, 25, 80, 100, 200];
// sort in ascending order
array.sort((a, b) => a - b);
console.log(array);

Excellent happy coding! 💫

👉 See my other projects on GitHub https://github.com/pH-7 💡

Would you like me to explain or break down the code?


This content originally appeared on DEV Community and was authored by Pierre-Henry Soria ✨


Print Share Comment Cite Upload Translate Updates
APA

Pierre-Henry Soria ✨ | Sciencx (2024-08-02T01:36:53+00:00) How to sort correctly arrays in JavaScript?. Retrieved from https://www.scien.cx/2024/08/02/how-to-sort-correctly-arrays-in-javascript/

MLA
" » How to sort correctly arrays in JavaScript?." Pierre-Henry Soria ✨ | Sciencx - Friday August 2, 2024, https://www.scien.cx/2024/08/02/how-to-sort-correctly-arrays-in-javascript/
HARVARD
Pierre-Henry Soria ✨ | Sciencx Friday August 2, 2024 » How to sort correctly arrays in JavaScript?., viewed ,<https://www.scien.cx/2024/08/02/how-to-sort-correctly-arrays-in-javascript/>
VANCOUVER
Pierre-Henry Soria ✨ | Sciencx - » How to sort correctly arrays in JavaScript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/02/how-to-sort-correctly-arrays-in-javascript/
CHICAGO
" » How to sort correctly arrays in JavaScript?." Pierre-Henry Soria ✨ | Sciencx - Accessed . https://www.scien.cx/2024/08/02/how-to-sort-correctly-arrays-in-javascript/
IEEE
" » How to sort correctly arrays in JavaScript?." Pierre-Henry Soria ✨ | Sciencx [Online]. Available: https://www.scien.cx/2024/08/02/how-to-sort-correctly-arrays-in-javascript/. [Accessed: ]
rf:citation
» How to sort correctly arrays in JavaScript? | Pierre-Henry Soria ✨ | Sciencx | https://www.scien.cx/2024/08/02/how-to-sort-correctly-arrays-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.