1

Input

var data = ["09 may 2015", "25 december 2015", "22 march 2015", "25 june 2016", "18 august 2015"];

output 22 March 2015, 09 May 2015, 18 August 2015, 25 December 2015, 25 jun 2016

6
  • 3
    Have you tried using .sort() and within the comparison using new Date() to get the actual dates? May be something like this: const sortData = arr => arr.sort((a, b) => (new Date(a)) > (new Date(b)) ? 1 : -1); console.log(sortData(data)); - please try and share feedback if any problem. Commented Mar 22, 2022 at 6:06
  • Using sort will produce the wrong result 09 may 2015 18 augest 2015 22 march 2015 25 december 2015 25 june 2016 Commented Mar 22, 2022 at 6:10
  • 1
    @jsN00b its working thanks Commented Mar 22, 2022 at 6:12
  • The reason it does not give "wrong result" is because the sort-function uses new Date() to convert the string into a JavaScript date and then compares the date. If we directly compare the strings, it'll give results that you've correctly noted. BTW, welcome bro (vaazthukkal & nandri). Commented Mar 22, 2022 at 6:15
  • Can we write it without using new date method Commented Mar 22, 2022 at 6:22

2 Answers 2

3

Subtracting 2 dates returns the difference between the two dates in milliseconds if a date is smaller than b returns negative values a will sorted to be a lower index than b.

var data = ["09 may 2015", "25 december 2015", "22 march 2015", "25 june 2016", "18 august 2015"];


data.sort(function(a,b){
  return  new Date(a) - new Date(b);
});
console.log(data);

Sign up to request clarification or add additional context in comments.

2 Comments

Mohammad Ali bhai, it is so good if you explain the function in brief. Thnx in advance.
@ShahzadBarkati Thanks for the query. I have added an explanation.
1

As noted in comments, one simple solution would be to use new Date(). Since OP requested for a possible method without new Date(), the below solution may be able to achieve the desired objective.

Code Snippet

// set-up a map/object to map "january": "01", "february": "02", etc
const monthMap = Object.fromEntries(
  "january,february,march,april,may,june,july,august,september,october,november,december"
  .split(',').map(
    (month, idx) => ([month, idx.toString().padStart(2, '0')])
  )
);

// use the map/object to convert month into 2-char string
const monthToNum = m => m.toLowerCase() in monthMap ? monthMap[m.toLowerCase()] : '99';

// reformat date from dd MMM yyyy to yyyy-mm-dd
const reformatDate = dt => {
  const parts = dt.split(' ');
  return [parts[2], monthToNum(parts[1]), parts[0]].join('-');
};

// use the custom-made "reformat" method to sort
const sortData = arr => arr.sort((a, b) => reformatDate(a) > reformatDate(b) ? 1 : -1);

// actual data from OP's question
const data = ["09 may 2015", "25 december 2015", "22 march 2015", "25 june 2016", "18 august 2015"];

// invoke the sort-method and console.log the result
console.log(sortData(data));

Explanation

Comments have been added in-line within the code-snippet above. If any further questions, please use comments below.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.