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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:08:38+00:00 2026-06-13T16:08:38+00:00

Using javascript sort() method, I am trying to do sorting a list but sorting

  • 0

Using javascript sort() method, I am trying to do sorting a list but sorting have in a group of even numbers and odd numbers.

The code which I tried is working fine with small list but if the list becomes big its not getting me proper result. Here is my code.

var n = [10,20,21,4,5,6,7,99,0,12,13];
//var n = [10,20,0,12];
n.sort(function(a,b){
 if (a % 2 !=b % 2 ){
   return a%2;
  }else {
    return a - b; 
  }
});

The above code gives me as per accepted result like this
Ans – [0, 4, 6, 10, 12, 20, 5, 7, 13, 21, 99] and the second Ans is:- [0, 10, 12, 20]

Working fine with this lists but if I changed into this

var n = [10,20,21,4,5,6,7,99,0,12,13,10,20,21,4,5,6,7,99,0,12,13,10,20,21,4,5,6,7,99,0,12,13,10,20,21,4,5,6,7,99,0,12,13];

But in this case the result is something like this which is not proper.

Giving me Ans like this

[0, 0, 0, 0, 4, 4, 4, 4, 6, 6, 6, 6, 10, 10, 10, 10, 12, 5, 12, 5, 12, 5, 20, 20, 20, 20, 5, 12, 7, 7, 7, 7, 13, 13, 13, 13, 21, 21, 21, 21, 99, 99, 99, 99] its a mixing of odd and even numbers.

Its giving me not proper result. Any suggestion.

  • 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-13T16:08:40+00:00Added an answer on June 13, 2026 at 4:08 pm

    The short of the shortest:

    n.sort(function(a, b) {
        return a % 2 - b % 2 || a - b;
    });
    

    To make it work with negative numbers we can add Math.abs():

    n.sort(function(a, b) {
        return Math.abs(a % 2) - Math.abs(b % 2) || a - b;
    });
    

    Or even more compact variant using bitwise AND:

    n.sort(function(a, b) {
        return (a & 1) - (b & 1) || a - b;
    });
    

    The most compact version (ES6):

    n.sort((a, b) => (a & 1) - (b & 1) || a - b);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I'm using JavaScript's native sort() method to do my sorting in
Background Using JavaScript, I need to sort a large JSON object based on a
Using JavaScript, is it possible to use prototype to add a method to a
Using JavaScript I'm trying to split a paragraph into it's sentences using regular expressions.
Using Javascript, I would like change the background color of the numbers in an
I've been programming for a few years but I'm new to JavaScript. I'm trying
I am reading some numbers from a local file and then trying to sort
I am using jQuery UI sortable to sort a list in the DOM. I
I'm trying to build a javascript array of objects, parsing dates using date.js, and
I know very little about JavaScript but despite this I'm trying to cobble something

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.