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

  • Home
  • SEARCH
  • 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 7697115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:56:09+00:00 2026-05-31T21:56:09+00:00

The JavaScript sort function which takes a parameter allows one to pass in a

  • 0

The JavaScript sort function which takes a parameter allows one to pass in a function.

For example:

var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return a - b}) //Array now becomes [7, 8, 25, 41]

How is it that the code

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

is interpreted to be ascending? It’s supposed to be divided into three cases, < 0 , == 0, and > 0, but how does this make sense when a and b can be anything?

Thank You!

  • 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-31T21:56:10+00:00Added an answer on May 31, 2026 at 9:56 pm

    The reason answering your question is especially tricky, or at least in detail, is because there is no specification that says which sorting algorithm a browser should implement. So telling you specifically how it works on one browser may differ between browsers, or even change over time.

    The gist of it is though, you want to think of “a” and “b” as being any two values. If you are returning the result of “a” – “b”, then your sort goes in ascending order. If you do “b” – “a”, then it is in descending order.

    The neat thing about making your own sort functions, is that you can compare the value of “a” and “b” after processing them in a separate function. So lets say you want to sort by the Celsius values, but your array in only in Fahrenheit. You can do something like:

    .sort(function(a,b){ return to_fahrenheit(a) - to_fahrenheit(b);}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
I am trying to sort a table with JavaScript. function test() { var table
function highest(){ return makeArray(arguments).sort(function(a,b){ return b - a; }); } function makeArray(array){ return Array().slice.call(
Is there any profiler sort of tool which can debug which javascript/object in the
The question sort of says it all - is there a function which does
The call to n.sort(sortNo) doesn't specify any parameters for the function sortNo (which defines
I'm using OpusScript, which is very similar to Javascript. I need to sort an
I have this function in javascript file which works perfectly fine in Firefox /
I have some javascript which will create some sort of widget on a page.
my goal is to create a sort of Javascript library, if you could call

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.