Array sort( ) method and comparator in Javascript

  1. It sorts the array elements in ascending order by default.
  2. It does not return a new array. It modifies the original array itself to its sorted form.
  3. It casts all the array elements as strings and then sorts them based on UTF-16 code unit values of the first character in the string.
  4. It accepts a comparator function as an argument. Custom logic can be written within the comparator function to sort the array based on different kinds of criteria.
  5. 5. It performs a stable sort.

It sorts the array elements in ascending order by default

It modifies the original array itself

It sorts the array based on the UTF-16 code unit values (unicode)

It accepts a comparator as an argument

  1. Numeric array
  2. String array
  3. Objects array

Sorting a numeric array

Sorting an array of strings

Sorting object property values

It performs a stable sort

Activity 1


Activity 2



Wrap Up





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Beginner’s guide to implement OOPS in React JS using Atomic Design Principles

Cover Image

Do more with less! Using render props

Phoenix 1.6 released!

How To Trigger a Scroll Event in Vue.js With the IntersectionObserver

Hello API

React: A Quick Introduction to useState

Photo of seven leaves in a line, showing the progression of autumn leaf colours. The left-most leaf is green and each leaf is in various stages of colour change; the final colour is red, as shown in the leaf on the right.

Set up multiple website in magento 2

6 Common Mistakes to Avoid During React Native Mobile App Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abheetha P

Abheetha P


More from Medium

The JavaScript bind method

JavaScript Simplified — Variables

My struggle with Javascript

JavaScript objects in depth.