Skip to content
Advertisements

Array sort()

JavaScript Array sort() Definition

The sort () method sorts the items in an array. This order (if we do not specify a function) is in the form of increasing order.

Syntax:

array.sort(arr)

arr: Defines the sort order in the array.

Let’s try to understand the logic with a small example.

        let names = ["rick", "oliver", "bob", "sasha"];
        let row = names.sort();
        console.log(row)

0:”bob”
1:”oliver”
2:”rick”
3:”sasha”
or
[“bob”,”oliver”,”rick”,”sasha”]

Sorting as shown in the example was according to the alphabet.

It is not right to use the sort () method for numbers. For example: When 10 and 20 numbers are listed more than 1 to 2, 10 is greater than 20. Therefore, the sort () method will produce the wrong result when sorting the numbers.

        let numbers = [2,4,1,3];
        let numbersRow = numbers.sort();
        console.log(numbersRow)

[1, 2, 3, 4]


JavaScript Array sort() Examples

Example 1

This code can be written to sort numbers with the sort () function:

        let numbers = [10, 20, 30, 40];
        let arr = numbers.sort(function (x, y) { return y - x })
        console.log(arr)

output:

[40, 30, 20, 10]


Example 2

        let names = [
            {name:"barry",born:2000},
            {name:"alex", born: 2003 },
            {name:"alice",born:2005}
        ]

        printRow();

        function row() {
            names.sort(function (x, y) { return x.born - y.born });
            printRow();
        }

        function printRow() {
            console.log(
            names[0].name + " " + names[0].born + "/" +
            names[1].name + " " + names[1].born + "/" +
            names[2].name + " " + names[2].born
            )
        }

output:

barry 2000 / alex 2003 / alice 2005


Example 3

        let names = [
            { name: "edward", age: 30 },
            { name: "alex", age: 25 },
            { name: "oliver", age: 32 },
            { name: "bob", age: 42 }
        ]

        let ageRow = names.sort(function (x, y) {
            return x.age - y.age;
        })

        let namesRow = names.sort(function (x, y) {
            let nameX = x.name.toUpperCase;
            let nameY = y.name.toUpperCase;
            if (nameX < nameY) { return -1 }
            if (nameX > nameY) { return 1 }
            return 0;
        })

        console.log(ageRow)
        console.log(namesRow)

output:

0: {name: “alex”, age: 25}
1: {name: “edward”, age: 30}
2: {name: “oliver”, age: 32}
3: {name: “bob”, age: 42}

0: {name: “alex”, age: 25}
1: {name: “edward”, age: 30}
2: {name: “oliver”, age: 32}
3: {name: “bob”, age: 42}


Chrome1
Edgeyes
Firefox1
Internet Explorer5.5
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes
Advertisements

2 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: