Home » Sorting arrays in Javascript using array.sort()
javascript array methods

Sorting arrays in Javascript using array.sort()

How to sort an array in Javascript using array.sort ?

Javascript comes with built in features and methods that can be useful for solving complex problems.

Using ES6 we can use now use native functions to sort an array e.g

// How to sort this array
// using javascript
const array = [2,1,3,6,7,54];

// output should be 

// [1,2,3,6,7,54]

before ES6 we used to use sorting algorithms for sorting arrays. The list of these algorithms were

  1. Bubble sort
  2. Binary Search Algorithm
  3. Quicksort
  4. Merge sort
  5. Insertion sort
  6. Selection sort
  7. Heap sort
  8. Radix sort
  9. Shell sort
  10. Counting sort
  11. Gnome sort
  12. Tree sort

However ES6 makes things easier and we don’t have to write any of those sorting algorithms anymore. So in this article, we will demonstrate how to sort an array along with examples.

1. Sorting array of number in ascending order using array.sort()

so we want to sort an array of [2,1,3,6,7,54] to [1,2,3,6,7,54], hence we will use array.sort() method to sort this array.


// How to sort this array
// using javascript
const array = [2,1,3,6,7,54];

const AsendingSort = 
array.sort((next, current)=> 
next - current);

// this has O(n)2 time complexity

// this has  O(n)2 space complexity

console.log({ AsendingSort});

// Output will be 

// [1, 2, 3, 6, 7, 54]

2. Sorting array of number in descending order using array.sort()

we want to sort an array of [2,1,3,6,7,54] to [54,7,6,3,2,1] into a descending order, we will use array.sort() method to sort this array.



// How to sort this array
// using javascript
const array = [2,1,3,6,7,54];

const DescendingSort = 
array.sort((next, current)=> 
current - next);

// this has O(n)2 time complexity

// this has  O(n)2 space complexity

console.log({ DescendingSort});

// Output will be 

// [54,7,6,3,2,1]


3. Sorting array of words in Alphabetical order using array.sort()

We often come with problems such as sorting words in Alphabetical orders and we want to sort them. we can use array.sort() for sorting them in alphabetical order. we want to sort

example:

['apple', 'xylem', 'zebra', 'banana']

to

['apple', 'banana', 'xylem', 'zebra']



// How to sort this array
// using javascript
const array = [
"apple", 
"xylem",
"zebra",
"banana"
];

const AlpabeticalSort = 
array.sort((next, current)=> 
next.localeCompare(current)
);

console.log({ AlpabeticalSort});

// Output will be 
/**

"apple", 
"banana"
"xylem",
"zebra",
*/


4. Sorting array of words in Alphabetical reverse order using array.sort()

Now it’s time to reverse the array of words. we can use array.sort() for sorting them in alphabetical reverse order. we want to sort

example:

['apple', 'xylem', 'zebra', 'banana']

to

[ 'zebra', 'xylem','banana', 'apple']



// How to sort this array
// using javascript
const array = [
"apple", 
"xylem",
"zebra",
"banana"
];

const ReversAlpabeticalSort = 
array.sort((next, current)=> 
current.localeCompare(next)
);

console.log({ ReversAlpabeticalSort});

// Output will be 
/**
"zebra",
"xylem", 
"banana"
"apple",
*/


5. Sorting array of object consisting names of books in Alphabetical order using array.sort()

We often come with problems such as sorting object of words in Alphabetical orders and we want to sort them. we can use array.sort() for sorting them in alphabetical order. we want to sort

example:

[{ bookName:'Oliver twist', price: 12.20},

{ bookName: 'Zebra Crossing the River', price: 10.60},

{ bookName :'Grand design', price: 20.00},

{ bookName :'All those pretty horses', price: 41.00}]

to

[{ bookName :'All those pretty horses', price: 41.00},

{ bookName :'Grand design', price: 20.00},

{ bookName:'Oliver twist', price: 12.20},

{ bookName: 'Zebra Crossing the River', price: 10.60}]

let’s look at this code example:



// How to sort this array
// using javascript

const array = [
  { 
  bookName: "Oliver twist", 
  price: 12.2 
  },
  { 
  bookName: "Zebra Crossing the River",
  price: 10.6
  },
  { 
  bookName: "Grand design",
  price: 20.0 
  },
  { 
  bookName: "All those pretty horses",
  price: 41.0 
  }
];
const AlpabeticalSort = 
array.sort((next, current) =>
next.bookName.localeCompare(next.current)
);
console.log({ AlpabeticalSort });

// Output will be
/**
[{ bookName :'All those pretty horses', 
price: 41.00},
{ bookName :'Grand design',
price: 20.00},
{ bookName:'Oliver twist', 
price: 12.20},
{ bookName: 'Zebra Crossing the River', 
price: 10.60}] 
*/



For more details have a look into this video

More Reading

Post navigation

2 Comments

  • Неllо all, guуѕ! I know, my mеssаge maу bе tоо spеcifiс,
    Βut my sister fоund nісe mаn hеrе and thеy married, ѕо how abоut mе?! 🙂
    I am 23 уears old, Μаrgarіtа, frоm Romania, Ι knоw Εnglish and Gеrmаn lаnguаgeѕ аlso
    Аnd… I havе spесіfic diѕеaѕe, named nymphomanіа. Ԝho knоw whаt іs thіs, сan undеrstand mе (bеttеr tо ѕау іt immediatеlу)
    Ah уeѕ, Ι cоok vеry taѕty! and Ι lоve nоt only сoоk ;))
    Ιm real girl, not рroѕtіtutе, аnd lоokіng for seriоus and hot rеlatiоnshір…
    Αnywaу, уou cаn fіnd my profilе hеrе: http://ipputopli.ga/page/40455/

  • I abslutely lovee youjr blog aand find almost all oof yokur post’s tto be what preciely I’m looking for.
    Do you odfer guest wwriters to write contdnt ffor you personally?
    I wouldn’t mid creating a post oor elaborating onn
    a few off the subjects yoou write with regards tto here.
    Again, awesome web log!

Leave a Reply

Your email address will not be published. Required fields are marked *

How Fetch works in JavaScript?

How to find keys of an object using javascript

How to authenticate pages in Vuejs (OIDC)

Mastering Loops in JavaScript: A Comprehensive Guide

Best Coding Ideas in JavaScript

How event loops works in Javascript?

Chat Icon