# Sort array of dates in javascript

##### javascript

Working with *date* in javascript is not very hard. The `Date`

object in javascript determines a single moment in time. It is platform-independent and returns a number as a result. This number represents milliseconds since 1 January 1970 UTC.

You can learn more about the javascript `Date`

object from MDN docs.

If you want to do any heavy lifting with date in javascript, then there is a library called momentJs. But for this particular task, we don't need any outside library or package. We can do it all by ourselves.

You can create a new `Date`

from scratch or parse `Date`

from `string`

with `Date.parse()`

method. But this parsing method is highly discouraged due to browser differences.

## How to sort an array in js

Before sorting dates, let's learn the basics first.

Let the alphabet is an array containing some initial English letters.

1let alphabat = ["c", "r", "w", "j", "g"];2

To sort this alphabet, run the sort() method in the array.

1alphabat.sort();2console.log(alphabat);34// Return: [ "c", "g", "j", "r", "w" ]5

Here you can see, that the array is now sorted as per their alphabetical order.

Now let's try to sort an array containing numbers.

1let numberList = [3, 5, 1, 0, 4, 7, 23, 15, 75, 10];2numberList.sort();3console.log(numberList);45// Return: [ 0, 1, 10, 15, 23, 3, 4, 5, 7, 75 ]6

Here the numbers are not correctly sorted in this example. This is not an error but javascript behaves that way. It checks the first digit of the number and sorts the numbers in this order.

Therefore we see 10, 15, and 23 come before 3 in the resultant array.

Now the question is if there is a solution? Yes. we have to define a comparing function inside the `sort()`

method. The comparing function looks like this.

1function(a,b){2 // Here you can define any algorithm of your choice.3 return a-b;4}5

If return from this function is > 0 then sort `a`

after `b`

. Similarly, if return value is < 0 the sort `a`

before `b`

. Finally, if the return value is == 0, then keep original order of `a`

and `b`

.

Therefore, our final number sorting code will look like this.

1let numberList = [3, 5, 1, 0, 4, 7, 23, 15, 75, 10];2numberList.sort(function (a, b) {3 return a - b;4});5console.log(numberList);67// Return: [ 0, 1, 3, 4, 5, 7, 10, 15, 23, 75 ]8

## Sort array of dates in js

Now, let us define an array of strings that are formatted as a date.

1let dates = ["2020-12-06", "2021-04-14", "2020-10-30"];2

As I have mentioned previously that dates are expressed as a number. Therefore we can use the previous comparison function to sort the dates as per our liking.

1let dates = ["2020-12-06", "2021-04-14", "2020-10-30"];23dates.sort(function (a, b) {4 // Converting string into Date object5 let date1 = new Date(a);6 let date2 = new Date(b);7 return date1 - date2;8});910console.log(dates);11// Return: [ "2020-10-30", "2020-12-06", "2021-04-14" ]12

Here you can see that our dates are perfectly sorted.

If you want to reverse the date array, then just use the `reverse()`

method on the `dates`

array.

1dates.reverse();2

## Conclusion

You have learned how to sort an array of dates in javascript. If you like this article, share it with your friends or colleagues.

If you like svelte and want to learn how to deploy a svelte app in github pages, then this article can give you a step by step guide to do so.

If you have any questions, I am on Twitter as @hrishikeshpathak. See you in another article.