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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:17:05+00:00 2026-06-06T13:17:05+00:00

var numArray = [4,2,5,3]; numArray.sort(function(a,b){ console.log(a: + a + , b: + b); return

  • 0
var numArray = [4,2,5,3];
numArray.sort(function(a,b){
  console.log("a:" + a + ", b:" + b);
  return a-b;
});

The three possible return numbers are: <0 (less than 0), 0, or >0 (greater than 0):

 Less than 0: Sort "a" to be a lower index than "b"
 Zero: "a" and "b" should be considered equal, and no sorting performed.
 Greater than 0: Sort "b" to be a lower index than "a".

I am getting like this on console

    a:4, b:2
    a:4, b:5
    a:5, b:3
    a:2, b:3
    a:4, b:3
    [2, 3, 4, 5]

May I know how the values of a and b are getting changed as seen above?

In the first step
a=4 and b=2. In this case it will swap.
then the array becomes [2,4,5,3];

In the second step
a=4 and b=5. In this case the items remains same.
then the array becomes [2,4,5,3];

In the third step
a=5 and b=3. In this case it will swap.
then the array becomes [2,4,3,5];

Until this step it is okay. But after that how the value of a and b becomes 2 and 3 > respectively instead of 2 and 4.

Anyone can help me?

Thanks in advance

  • 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-06-06T13:17:06+00:00Added an answer on June 6, 2026 at 1:17 pm

    There is no guarantee that the sort method uses a simple compare and swap algorithm.

    More likely your browser uses something like merge sort, but the specific algorithm will depend on the browser and version.

    Since Firefox is open source you can see how sort is implemented by checking the source code. It uses a merge sort.

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

Sidebar

Related Questions

var AppPatientsList = JSON.parse(JSON RESPONSE); var AppPatientsListSort = AppPatientsList.sort(function(a,b){ return a.firstName.toLowerCase() <b.firstName.toLowerCase() ? -1
var h=0; var w=0; $(.extend).hover(function() { //alert(h); $(this).css({'z-index' : '999' }); $(this).addClass(hover).filter(:not(:animated)).stop() .animate({ marginTop:
var $thingy = $('.thingy'), $window = $(window); $window.on('resize', function(){ thingy.width($window.width()/12); }); Is it possible
var ddd = Math.random() * 16; console.log((ddd & 3 | 8).toString(16)); Help me please.
var parser = new DOMParser(), d = parser.parseFromString('<?xml version=1.0?><div class=a>Hello</div>', 'application/xhtml+xml'); console.log(d.querySelector('*')); console.log(d.querySelector('div')); console.log(d.querySelector('div.a'));
var results = ['one', 'two', 'one hundred', 'three']; var removal = []; $.each(results, function(i)
var conversations = new Array(); jQuery('.CChatWindow').each(function(){ if (jQuery(this).is(:visible) && jQuery(this).attr(data-conversationid) != 0) { alert(jQuery(this).attr(data-conversationid));
var chart; var d = new Array(); function click() { d = document.getElementById('graph:hi').value; alert(d);
var positions = [ 00, 01, 02, 03, 04, 05 ]; jQuery.each( positions, function(players)
var v1 = _questionRepository.GetQuery().Where(q => q.EvaluationGroupId == evaluationGroupId && ((q.TopicValue == questionTopic && q.Index

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.