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 990747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:59:27+00:00 2026-05-16T05:59:27+00:00

Recently when I was working with JavaScript sort() function, I found in one of

  • 0

Recently when I was working with JavaScript “sort()” function, I found in one of the tutorials that this function does not sort the numbers properly. Instead to sort numbers, a function must be added that compares numbers, like the following code:-

<script type="text/javascript">
function sortNumber(a,b)
{
    return a - b;
}

var n = ["10", "5", "40", "25", "100", "1"];
document.write(n.sort(sortNumber));
</script>

The output then comes as:-

1,5,10,25,40,100

Now what I didn’t understand is that why is this occurring & can anybody please tell in details as to what type of algorithm is being used in this “sort()” function? This is because for any other language, I didn’t find this problem where the function didn’t sort the numbers correctly.

Any help is greatly appreciated.

  • 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-16T05:59:28+00:00Added an answer on May 16, 2026 at 5:59 am

    Well, if you are sorting the following list, it contains only strings:

    var n = ["10", "5", "40", "25", "100", "1"];
    

    So I would expect any language would compare them as strings, thus resulting in a sort order of:

    var n = ["1", "10", "100", "25", "40", "5"];
    

    Which necessitates your code to use a custom sort (as you have done) to cast the strings back to integers for the purposes of sorting.

    Edit

    As Pointy mentioned, by default the JavaScript sort() method sorts elements alphabetically, including numbers:

    By default, the sort() method sorts the elements alphabetically and ascending. However, numbers will not be sorted correctly (40 comes before 5). To sort numbers, you must add a function that compare numbers.

    Simply amazing… so a custom sort is required even for an array of integers.

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

Sidebar

Related Questions

Recently I was working with some JavaScript code and I found this: <script type=text/javascript>
I recently started working with a client that had an index.html file with this
I recently asked a question relating to the javascript not working on my site
I recently ran into this problem: Rails 3 link_to (:method => :delete) not working
I read some code recently that does something like this: bob = {'name': 'Bob
I got this code on here recently <script type='text/javascript'> $('#discountselection').hide(); $('#No').click(function(){ $('#discountselection').hide(); }); $('#Yes').click(function(){
Recently, using javascript(jQuery to be specific) I was working on a script that retrieves
I've been working with some event handling in Javascript and recently decided to migrate
I am recently working on better indexing concepts. as part of that, I am
I'm recently working on a WPF application that uses the datagrid from the WPF

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.