Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Array sort() Method

Posted on December 16, 2024 By Admin No Comments on JavaScript Array sort() Method

The Array.sort() method is a built-in JavaScript function used to sort the elements of an array in place. It organizes the elements either in ascending or descending order based on a specified compare function.

By default, the sort() method converts elements to strings and sorts them lexicographically, which can lead to unexpected results when sorting numbers.

Syntax

arr.sort(compareFunction);

Parameters

ParameterDescription
compareFunction(Optional) A function that defines the sort order. If omitted, elements are converted to strings and sorted lexicographically.

Return Value

The sort() method returns the sorted array. The original array is modified.

Example 1: Default Lexicographic Sorting

let arr = ['banana', 'apple', 'mango', 'cherry'];

arr.sort();
console.log(arr); 
// Output: ['apple', 'banana', 'cherry', 'mango']

By default, sort() method arrange elements in lexicographic (dictionary) order.

Example 2: Sorting Numbers Without a Compare Function

let arr = [10, 101, 1, 25, 3];

arr.sort();
console.log(arr); 
// Output: [1, 10, 101, 25, 3]

Without a compare function, numbers are treated as strings and sorted lexicographically, resulting in incorrect numerical order.

Example 3: Sorting Numbers with a Compare Function

let arr = [10, 101, 1, 25, 3];

arr.sort((a, b) => a - b);
console.log(arr); 
// Output: [ 1, 3, 10, 25, 101 ]

Here, the compare function a - b ensures correct numerical sorting in ascending order.

Example 4: Descending Order Sorting

let arr = [10, 101, 1, 25, 3];
arr.sort((a, b) => b - a);
console.log(arr); 
// Output: [101, 25, 10, 3, 1]

By reversing the order in the compare function (b - a), the array is sorted in descending order.

Example 5: Sorting an Array of Strings with Case Sensitivity

let arr = ['banana', 'apple', 'Apple', 'cherry'];
arr.sort();
console.log(arr); 
// Output: [ 'Apple', 'apple', 'banana', 'cherry' ]

By default, sort() is case-sensitive and places uppercase letters before lowercase ones due to Unicode values.

Example 6: Sorting an Array of Objects

let users = [
  { name: 'John', age: 25 },
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 20 }
];

users.sort((a, b) => a.age - b.age);
console.log(users);
/* Output: [
    { name: 'Bob', age: 20 },
    { name: 'John', age: 25 },
    { name: 'Alice', age: 30 }
]  */

Supported Browsers

BrowserSupport
Chrome1+
Firefox1+
Safari1+
Edge12+
Opera4+
Internet Explorer5.5+

JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array.length Property
Next Post: JavaScript Array pop() Method

Leave a Reply Cancel reply

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

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme