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

156
Views
Why Isnt My Array (Based On Time) Being Ascendency Sorted?

I have this script here

let myVar = [{time: "3:00 pm", name: "Landon"},{time: "5:20 pm", name: "Amanda"},{time: "4:00 pm", name: "Kid"}]
const sortedArray  = myVar.sort((a,b) => new moment(a.time).format('hh:mm a') - new moment(b.time).format('hh:mm a'))
console.log(sortedArray)

The idea is that we sort the array by time. But its not working and Im not sure why.

Every google search leads me to answers like this: https://gist.github.com/onildoaguiar/6cf7dbf9e0f0b8684eb5b0977b40c5ad

But it does not work for me.

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

0

Your code passes a format at the wrong moment. You need to specify the input format, not the output format.

So change to:

let myVar = [{time: "3:00 pm", name: "Landon"},{time: "5:20 pm", name: "Amanda"},{time: "4:00 pm", name: "Kid"}]
const sortedArray  = myVar.sort((a,b) => new moment(a.time, 'hh:mm a') - new moment(b.time, 'hh:mm a'))
console.log(sortedArray)      
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js" ></script>

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!