Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Sort Arr Numbers MM/DD/YYYY When they're strings?

How do I sort these descending correctly? I've tried arr.sort() arr.sort().reverse() , looked all over stack overflow ... and I cannot find a way to do this. Everything tries to sort them but does it wrong.

[
  '1/19/2024',  '1/20/2023',
  '10/21/2022', '11/18/2022',
  '12/16/2022', '3/17/2023',
  '6/10/2022',  '6/16/2023',
  '6/17/2022',  '6/21/2024',
  '6/24/2022',  '6/3/2022',
  '6/31/2022',  '7/15/2022',
  '7/8/2022',   '8/19/2022',
  '9/15/2023',  '9/16/2022'
]

Then I need to take those and turn them into the actual month. Jan, Feb, etc. instead of the numerical value ...

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

So, easiest way is to convert strings to dates, sort it and then convert dates to string format back, see:

const origArr = [
  '1/19/2024', '1/20/2023',
  '10/21/2022', '11/18/2022',
  '12/16/2022', '3/17/2023',
  '6/10/2022', '6/16/2023',
  '6/17/2022', '6/21/2024',
  '6/24/2022', '6/3/2022',
  '6/31/2022', '7/15/2022',
  '7/8/2022', '8/19/2022',
  '9/15/2023', '9/16/2022'
];

// create array of dates
const datesArr = origArr.map(str => new Date(str));

// sort array of dates
const datesSortedArr = datesArr.sort((a,b) => b - a);

// convert dates to strings
const sortedArr = datesSortedArr.map(item => {
  const month = item.getMonth() + 1;
  const day = item.getDate();
  const year = item.getFullYear();
  
  return `${month}/${day}/${year}`;
});
console.log('sortedArr:', sortedArr);

about 4 years ago · Juan Pablo Isaza Report

0

you can convert them to Date order them and than convert them back to the original format

const dates = [
  '1/19/2024',  '1/20/2023',
  '10/21/2022', '11/18/2022',
  '12/16/2022', '3/17/2023',
  '6/10/2022',  '6/16/2023',
  '6/17/2022',  '6/21/2024',
  '6/24/2022',  '6/3/2022',
  '6/31/2022',  '7/15/2022',
  '7/8/2022',   '8/19/2022',
  '9/15/2023',  '9/16/2022'
]

const toDate = string => {
 const [month, day, year] = string.split('/')
 
 return new Date([year, month.padStart(2, '0'), day.padStart(2, '0')].join('-'))
}


const toString = date => [date.getMonth() + 1, date.getDate(), date.getFullYear()].join('/')

const reorder = data => {
 const dates = data.map(toDate)
 dates.sort((a, b) => b - a)
 
 return dates.map(toString)
}

console.log(reorder(dates))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!