Languages
[Edit]
EN

JavaScript - universal way to compare values (boolean, number, string, Date, etc.)

6 points
Created by:
Lani-Skinner
748

In this article, we're going to have a look at how to compare two values when we want to sort them in JavaScript.

Presented comparison result we should understand the following way:

  • -1 when first value is smaller than second one,
  • 0 when values are equal,
  • +1 when first value is greater than second one.

Quick solution:

Note: the presented function compares in correct way ISO 8601 string dates also.

 

Practical example

Edit

 

Sort array example

Edit

 

Alternative titles

  1. JavaScript - universal way to sort values in array (booleans, numbers, strings, dates, etc.)
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join