peirix
peirix

Reputation: 37771

How to sort an array of integers?

Trying to get the highest and lowest value from an array that I know will contain only integers seems to be harder than I thought.

var numArray = [140000, 104, 99];
numArray = numArray.sort();
console.log(numArray)

I'd expect this to show 99, 104, 140000. Instead it shows 104, 140000, 99. So it seems the sort is handling the values as strings.

Is there a way to get the sort function to sort on the integer value of each array element?

Upvotes: 1455

Views: 1304734

Answers (30)

Lajos Arpad
Lajos Arpad

Reputation: 76943

Since comparison and sorting is way more broader topic than comparing and sorting integers, it's good to implement a convert function that ensures the type to be as expected, see the second example as well as a compare function that properly compares your items. So this will be reusable instead of duplicating the code. In the snippet below we see the test for three different examples, inspired from the original example.

function convert(value, type) {
    switch (type) {
        case 'int': return parseInt(value);
        //other types here
        default: return value;
    }
}

function compare(a, b, type, ascending = true) {
    switch (type) {
        case 'int': return (ascending ? 1 : -1) * (a - b);
        default: return (ascending ? 1 : -1) * (a - b)
    }
}

var numArray = [140000, 104, 99];
numArray = numArray.sort(function(a, b) {
    return compare(convert(a, 'int'), convert(b, 'int'), 'int');
});
console.log(numArray)
numArray = ["140000", "104", "99"];
numArray = numArray.sort(function(a, b) {
    return compare(convert(a, 'int'), convert(b, 'int'), 'int');
});
console.log(numArray)
numArray = ["140000", "104", 99];
numArray = numArray.sort(function(a, b) {
    return compare(convert(a, 'int'), convert(b, 'int'), 'int', false);
});
console.log(numArray)

Upvotes: 0

dy_
dy_

Reputation: 7003

Passing a comparator function to sort() is slower than creating any TypedArray:

var numArray = new Float64Array([140000, 104, 99]);
numArray = numArray.sort();
console.log(numArray)

Upvotes: 177

vitoboski
vitoboski

Reputation: 223

1. Ascending

const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];

If we return something < 0 then A will be before B If we return something > 0 then B will be before A

movements.sort((a, b) => {
    if (a > b) return 1; //- (Switch order)
    if (a < b) return -1; //- (Keep order)
});

a - current value, b - the next value.

2. Descending

movements.sort((a, b) => {
    if (a > b) return -1; // - (Keep)
    if (a < b) return 1; // - (Switch)
}); 

Improve, best solution

movements.sort ((a, b) => a - b); // Same result!

If a < b it's negative number.

Upvotes: 0

Ilyich
Ilyich

Reputation: 5796

This can be done with localeCompare using either options or locales tag:

const numArray = [140000, 104, 99, 3, 2, 12, 25, 10, 1]

console.log('Using options:',
  numArray.sort((a, b) => a.toString().localeCompare(b, undefined, { numeric: true })),
)

console.log('Using locales tag:',
  numArray.sort((a, b) => a.toString().localeCompare(b, 'en-u-kn-true')),
)

Upvotes: 0

norbekoff
norbekoff

Reputation: 1965

just do .sort((a, b) => a - b) instead of .sort() itself. In addition to that the array is sorted in place. So return value does not matter.

var numArray = [140000, 104, 99];
numArray.sort((a, b) => a - b);
console.log(numArray)

Upvotes: 51

Sangwin Gawande
Sangwin Gawande

Reputation: 8166

Array.sort uses alphabetic sorting by default instead of numeric .

To support numbers, add like following

var numArray = [140000, 104, 99];
numArray.sort((a, b) =>  a - b); // <-- Ascending
numArray.sort((a, b) =>  b - a); // <-- Descending
console.log(numArray);

OUTPUT :

Array Numeric Sorting

Upvotes: 22

aks
aks

Reputation: 25377

By default, the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts (sortNumber, shown below) -

var numArray = [140000, 104, 99];
numArray.sort(function(a, b) {
  return a - b;
});

console.log(numArray);

Documentation:

Mozilla Array.prototype.sort() recommends this compare function for arrays that don't contain Infinity or NaN. (Because Infinity - Infinity is NaN, not 0).

Also examples of sorting objects by key.

Upvotes: 2044

twizelissa
twizelissa

Reputation: 161

let grade =[80,100,50,90,40];
grade.sort((x,y)=> x-y);
grade.forEach(element=>console.log(element));

Upvotes: 1

Tesla
Tesla

Reputation: 189

If you need to calculate and sort the largest charCodeAt from a list of string this is the right way.

const arrayLines = '1.1.1.1\n1.0.1.1\n1.1.1.2\n1.1.1.0'.split('\n');

// Response: (4) ['1.0.1.1', '1.1.1.0', '1.1.1.1', '1.1.1.2']
arrayLines.sort((a, b) => {
    let a_charCodeSize = 0,
        b_charCodeSize = 0;

    // Loop true a & b characters and calculate the charCodeAt size.
    for (const aChar of a) a_charCodeSize += aChar.charCodeAt(0);
    for (const bChar of b) b_charCodeSize += bChar.charCodeAt(0);

    return a_charCodeSize - b_charCodeSize;
});

Upvotes: 0

Coffezilla
Coffezilla

Reputation: 386

In order to create this kind of sort, you have to pass a function that will check which comes first.

define inside the function which value do you wanna check: a.id - a.id

        const myJson = [
            { id: 1, name: 'one'},
            { id: 4, name: 'four'},
            { id: 2, name: 'two'},
            { id: 3, name: 'three'}
        ];

        // provide the sort method to check
        const myNewSort = myJson.sort(function(a, b) {
          return a.id - b.id;
        });

        console.log('my new sort',myNewSort)

Upvotes: 3

R.M. Reza
R.M. Reza

Reputation: 1009

Ascending

arr.sort((a, b) => a - b);

Descending

arr.sort((a, b) => b - a);

Just for fun:

Descending = Ascending + Reverse

arr.sort((a, b) => a - b).reverse();

Upvotes: 23

Adam M. Costello
Adam M. Costello

Reputation: 176

The accepted answer and equivalents like numArray.sort((a,b) => a - b) are great when the array contains only numbers without infinities or NaN. They can be extended to handle infinities and NaN like so:

numArray.sort((a,b) => (+a || 0) - (+b || 0) || 0);

This sorts NaN (or any non-number, like 'foo' or {}) as if it were 0. The final || 0 is needed to handle the case where a and b are equal infinities.

Upvotes: 6

MarzSocks
MarzSocks

Reputation: 4318

Just building on all of the above answers, they can also be done in one line like this:

var numArray = [140000, 104, 99];
numArray = numArray.sort(function (a, b) {  return a - b;  });

//outputs: 99, 104, 140000

Upvotes: 223

MD SHAYON
MD SHAYON

Reputation: 8063

You can get height and lowest number simply by using max() and min() in-built function

var numArray = [140000, 104, 99];
console.log(Math.max(...numArray));
console.log(Math.min(...numArray));

If you want to sort in ascending or descending order

numArray.sort((a, b)=> a - b);

Know more

Upvotes: 1

Chris Kobrzak
Chris Kobrzak

Reputation: 1412

TypeScript variant

const compareNumbers = (a: number, b: number): number => a - b

myArray.sort(compareNumbers)

Upvotes: 3

hasanga lakdinu
hasanga lakdinu

Reputation: 154

You can sort number array simply by

const num=[13,17,14,19,16];
let temp;
for(let i=0;i<num.length;i++){
    for(let j=i+1;j<num.length;j++){
        if(num[i]>num[j]){
            temp=num[i]
            num[i]=num[j]
            num[j]=temp
        }
    }
}

console.log(num);

Upvotes: 1

Mejan
Mejan

Reputation: 1276

If anyone doesn't understand how Array.sort() works with integers, read this answer.

Alphabetical order:

By default, the sort() method sorts the values as strings in alphabetical and ascending order.

const myArray = [104, 140000, 99];
myArray.sort();
console.log(myArray); // output is [104, 140000, 99]

Ascending order with array.sort(compareFunction):

const myArray = [104, 140000, 99];
myArray.sort(function(a, b){
  return a - b;
});
console.log(myArray); // output is [99, 104, 140000]

Explanation from w3schools:

compareFunction defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like: function(a, b){return a-b} When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

Example:

When comparing 40 and 100, the sort() method calls the compare function(40,100).

The function calculates 40-100, and returns -60 (a negative value).

The sort function will sort 40 as a value lower than 100.

Descending order with array.sort(compareFunction):

const myArray = [104, 140000, 99];
myArray.sort(function(a, b){
  return b - a;
});
console.log(myArray); // output is [140000, 104, 99]

This time we calculated with b - a(i.e., 100-40) which returns a positive value.

Upvotes: 0

bortunac
bortunac

Reputation: 4828

sort_mixed

Object.defineProperty(Array.prototype,"sort_mixed",{
    value: function () { // do not use arrow function
        var N = [], L = [];
        this.forEach(e => {
            Number.isFinite(e) ? N.push(e) : L.push(e);
        });
        N.sort((a, b) => a - b);
        L.sort();
        [...N, ...L].forEach((v, i) => this[i] = v);
        return this;
    })

try a =[1,'u',"V",10,4,"c","A"].sort_mixed(); console.log(a)

Upvotes: 0

Putzi San
Putzi San

Reputation: 6341

Sort integers > 0, think outside the box:

function sortArray(arr) {
  return new Promise((resolve) => {
    const result = []
    arr.forEach((item) => {
      setTimeout(() => {
        result.push(item)
        if (result.length === arr.length) resolve(result)
      }, item)
    })
  })
}

sortArray([4, 2, 42, 128, 56, 2]).then((result) => {
  document.write(JSON.stringify(result))
})

Note that this should not be used productively, .sort() is better suited for this, check the other answers

Upvotes: 1

Grant Miller
Grant Miller

Reputation: 29047

While not required in JavaScript, if you would like the sort() compareFunction to strictly return -1, 0, or 1 (similar to how the spaceship operator works in PHP), then you can use Math.sign().

The compareFunction below strictly returns -1, 0, or 1:

numArray.sort((a, b) => Math.sign(a - b));

Note: Math.sign() is not supported in Internet Explorer.

Upvotes: 4

Anshul Chaurasia
Anshul Chaurasia

Reputation: 100

As sort method converts Array elements into string. So, below way also works fine with decimal numbers with array elements.

let productPrices = [10.33, 2.55, 1.06, 5.77];
console.log(productPrices.sort((a,b)=>a-b));

And gives you the expected result.

Upvotes: 0

stackphish
stackphish

Reputation: 163

The function 'numerically' below serves the purpose of sorting array of numbers numerically in many cases when provided as a callback function:

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

array.sort(numerically); 

But in some rare instances, where array contains very large and negative numbers, an overflow error can occur as the result of a-b gets smaller than the smallest number that JavaScript can cope with.

So a better way of writing numerically function is as follows:

function numerically(a, b){
   if(a < b){
      return -1;
   } else if(a > b){
      return 1;
   } else {
      return 0;
   }
}

Upvotes: 9

Ali Khosro
Ali Khosro

Reputation: 1830

to handle undefined, null, and NaN: Null behaves like 0, NaN and undefined goes to end.

array = [3, 5, -1, 1, NaN, 6, undefined, 2, null]
array.sort((a,b) => isNaN(a) || a-b)
// [-1, null, 1, 2, 3, 5, 6, NaN, undefined]

Upvotes: 6

Black
Black

Reputation: 20342

The reason why the sort function behaves so weird

From the documentation:

[...] the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.

If you print the unicode point values of the array then it will get clear.

console.log("140000".charCodeAt(0));
console.log("104".charCodeAt(0));
console.log("99".charCodeAt(0));

//Note that we only look at the first index of the number "charCodeAt(  0  )"

This returns: "49, 49, 57".

49 (unicode value of first number at 140000)
49 (unicode value of first number at 104)
57 (unicode value of first number at 99)

Now, because 140000 and 104 returned the same values (49) it cuts the first index and checks again:

console.log("40000".charCodeAt(0));
console.log("04".charCodeAt(0));

//Note that we only look at the first index of the number "charCodeAt(  0  )"

52 (unicode value of first number at 40000)
40 (unicode value of first number at 04)

If we sort this, then we will get:

40 (unicode value of first number at 04)
52 (unicode value of first number at 40000)

so 104 comes before 140000.

So the final result will be:

var numArray = [140000, 104, 99];
numArray = numArray.sort();
console.log(numArray)

104, 140000, 99

Conclusion:

sort() does sorting by only looking at the first index of the numbers. sort() does not care if a whole number is bigger than another, it compares the value of the unicode of the digits, and if there are two equal unicode values, then it checks if there is a next digit and compares it as well.

To sort correctly, you have to pass a compare function to sort() like explained here.

Upvotes: 35

P.S.
P.S.

Reputation: 16384

The question has already been answered, the shortest way is to use sort() method. But if you're searching for more ways to sort your array of numbers, and you also love cycles, check the following

Insertion sort

Ascending:

var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length; i++) {
    var target = numArray[i];
    for (var j = i - 1; j >= 0 && (numArray[j] > target); j--) {
        numArray[j+1] = numArray[j];
    }
    numArray[j+1] = target
}
console.log(numArray);

Descending:

var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length; i++) {
    var target = numArray[i];
    for (var j = i - 1; j >= 0 && (numArray[j] < target); j--) {
        numArray[j+1] = numArray[j];
    }
    numArray[j+1] = target
}
console.log(numArray);

Selection sort:

Ascending:

var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length - 1; i++) {
    var min = i;
    for (var j = i + 1; j < numArray.length; j++) {
        if (numArray[j] < numArray[min]) {
            min = j;
        }
    }
    if (min != i) {
        var target = numArray[i];
        numArray[i] = numArray[min];
        numArray[min] = target;
    }
}
console.log(numArray);

Descending:

var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length - 1; i++) {
    var min = i;
    for (var j = i + 1; j < numArray.length; j++) {
        if (numArray[j] > numArray[min]) {
            min = j;
        }
    }
    if (min != i) {
        var target = numArray[i];
        numArray[i] = numArray[min];
        numArray[min] = target;
    }
}
console.log(numArray);

Have fun

Upvotes: 14

user7125929
user7125929

Reputation: 85

Try this code as below

var a = [5, 17, 29, 48, 64, 21];
function sortA(arr) {
return arr.sort(function(a, b) {
return a - b;
})
;} 
alert(sortA(a));

Upvotes: 2

Umesh
Umesh

Reputation: 971

For a normal array of elements values only:

function sortArrayOfElements(arrayToSort) {
    function compareElements(a, b) {
        if (a < b)
            return -1;
        if (a > b)
            return 1;
        return 0;
    }

    return arrayToSort.sort(compareElements);
}

e.g. 1:
var array1 = [1,2,545,676,64,2,24]
**output : [1, 2, 2, 24, 64, 545, 676]**

var array2 = ["v","a",545,676,64,2,"24"]
**output: ["a", "v", 2, "24", 64, 545, 676]**

For an array of objects:

function sortArrayOfObjects(arrayToSort, key) {
    function compareObjects(a, b) {
        if (a[key] < b[key])
            return -1;
        if (a[key] > b[key])
            return 1;
        return 0;
    }

    return arrayToSort.sort(compareObjects);
}

e.g. 1: var array1= [{"name": "User4", "value": 4},{"name": "User3", "value": 3},{"name": "User2", "value": 2}]

**output : [{"name": "User2", "value": 2},{"name": "User3", "value": 3},{"name": "User4", "value": 4}]**

Upvotes: 3

Merbin Joe
Merbin Joe

Reputation: 688

Array.prototype.sort() is the go to method for sorting arrays, but there are a couple of issues we need to be aware of.

The sorting order is by default lexicographic and not numeric regardless of the types of values in the array. Even if the array is all numbers, all values will be converted to string and sorted lexicographically.

So should we need to customize the sort() and reverse() method like below.

Referred URL

For sorting numbers inside the array

numArray.sort(function(a, b)
{
    return a - b;
});

For reversing numbers inside the array

numArray.sort(function(a, b)
{
    return b - a;
});

Referred URL

Upvotes: 9

jjjjs
jjjjs

Reputation: 1650

This answer is equivalent to some of the existing answers, but ECMAScript 6 arrow functions provide a much more compact syntax that allows us to define an inline sort function without sacrificing readability:

numArray = numArray.sort((a, b) => a - b);

It is supported in most browsers today.

Upvotes: 86

Sunny S.M
Sunny S.M

Reputation: 5998

Try this code:

HTML:

<div id="demo"></div>

JavaScript code:

<script>
    (function(){
        var points = [40, 100, 1, 5, 25, 10];
        document.getElementById("demo").innerHTML = points;
        points.sort(function(a, b){return a-b});
        document.getElementById("demo").innerHTML = points;
    })();
</script>

Upvotes: 2

Related Questions