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

262
Views
what does - do in the sort function? why are we subtracting b from a?
Array.sort((a,b) => a - b);

so i have this typical sort for an Array of numbers. What does the subtract do? why are we subtracting b from a?

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

0

The sort function returns a number, because values greater than 0 are sorting b before a and values less than 0 are sorting a before b.

So if you are returning a-b you are sorting in ascending order (from small to big numbers)

You can read more about it here: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

about 4 years ago · Juan Pablo Isaza Report

0

If the result is >0 :

b goes before a

If the result is <0 :

a goes before b

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!