Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:56:20+00:00 2026-05-23T12:56:20+00:00

As already asked: how does sort function work in JavaScript, along with the compare

  • 0

As already asked: how does sort function work in JavaScript, along with the compare function?
If I have an array, and I do array.sort(compare) now it was written in the book that if the compare function returns a-b (two indices of the array) then it works based on the fact that whether the result is greater than 0, less than 0 or equal to 0. But, how exactly does it work? I could not work it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:56:21+00:00Added an answer on May 23, 2026 at 12:56 pm

    The “compare” function must take two arguments, often referred to as a and b. Then you make the compare function return 0, greater than 0, or less than 0, based on these values, a and b.

    1. Return greater than 0 if a is greater than b
    2. Return 0 if a equals b
    3. Return less than 0 if a is less than b

    With these three return values, and only two arguments, it is possible to write a compare function that can sort any type of input data type, or complex data structures.

    Then, when you call sort(), with your custom compare function, the compare function is called on pairs in your to-be-sorted list, to determine the proper ordering.

    Lets walk through a simple example… Suppose you’re only sorting some numbers, so we have a very simple compare function:

    function compare(a,b) {
        return a - b;
    }
    

    Simply subtracting b from a will always return greater than zero if a is larger than b, 0 if they are equal, or less than zero if a is less than b. So it meets the requirements for a compare function.

    Now lets suppose this is our list of numbers to sort:

    var numbers = [1,5,3.14];
    

    When you call numbers.sort(compare), internally it will actually execute:

    compare(1,5);     // Returns -4, a is less than b
    compare(1,3.14);  // Return -2.14, a is less than b
    compare(5,3.14);  // returns 1.86, a is greater than b
    

    If you’ve ever done manual sorting or alphabetizing, you’ve done precisely the same thing, probably without realizing it. Even though you may have dozens or hundreds of items to compare, you’re constantly comparing only two numbers (or author’s last names, or whatever) at a time. Going through or short list of three numbers again, you’d start by comparing the first two numbers:

    1. Is 1 greater than or less than 5? Less than, so put these two numbers in our list: 1,5
    2. Is 3.14 greater than or less than 1? Greater than, so it goes after 1 in the new list
    3. Is 3.14 greater than or less than 5 in our new list? Less than, so it goes before 5. Our new list is now [1,3.14,5]

    Because you can provide your own compare() function, it is possible to sort arbitrarily complex data, not just numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: I have a question that is apparently not answered by this already-asked Bash
Someone has already asked, How does the DiggBar work? in a previous question. While
I already asked this question at the JOS-.NET board but Joel is closing that
Previously I've asked about inserting a column into a dataset . I now have
Already asked on the Evil Exchange, but as always that was no help. I'm
Apologies if this was already asked but, I have had a look and can't
I'm sure that this question has already been asked, but I don't really see
A similar question was already asked ( Performing a Stress Test on Web Application?
I know a question like this was already asked, but the situation is a
If this question has already been asked I appologies, please point me in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.