---
title: "What is the role of Array.sort() in JS?"  
description: "What is the role of Array.sort() in JS?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93741/what-is-the-role-of-array-sort-in-js  
category: "web application"  
tags: ["javascript", "web application development", "web development"]  
reading_time: 1 minute  

---

# What is the role of Array.sort() in JS?

What is the [role](https://yourviews.mindstick.com/view/85446/national-doctor-s-day-recognizing-the-indispensable-role-of-doctors-in-one-s-life) of Array.sort() in JS?

## Answers

### Answer by Ethan Karla

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings. **Syntax**

```
// Functionless
sort()
// Arrow function
sort((firstEl, secondEl) => { ... } )
// Compare function
sort(compareFn)
// Inline compare function
sort(function compareFn(firstEl, secondEl) { ... })
```

**Example:**

```
const fruits = ['BANANA','APLLE','orang','grapes','mango'];
    fruits.sort();
    console.log(fruits);
    // expected output: Array ['Dec', 'Feb', 'Jan', 'March']
const array1 = [54,21,32,5,1,21];
const array=   array1.sort(function(a,b){
    return a-b;
});
    console.log(array);
```

\


---

Original Source: https://answers.mindstick.com/qa/93741/what-is-the-role-of-array-sort-in-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
