JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Array Sort


📘 JavaScript Array Sort – Sorting Arrays Alphabetically and Numerically

JavaScript provides the sort() method to sort array elements in place, which means it modifies the original array.

By default, sort() treats values as strings, even if they are numbers.


🔹 Syntax

array.sort([compareFunction])
  • Without compareFunction: sorts elements as strings

  • With compareFunction: you define how elements should be compared


🔹 Default Sorting (Lexicographical)

let fruits = ["Banana", "Apple", "Mango"];
fruits.sort();  // ["Apple", "Banana", "Mango"]

🔴 Sorting numbers without a compare function:

let nums = [25, 100, 5, 40];
nums.sort();     // [100, 25, 40, 5] ❌ (Incorrect for numbers)

🔹 Numeric Sort (Ascending & Descending)

✅ Correct way to sort numbers:

let numbers = [25, 100, 5, 40];

// Ascending
numbers.sort((a, b) => a - b);     // [5, 25, 40, 100]

// Descending
numbers.sort((a, b) => b - a);     // [100, 40, 25, 5]

🔹 Reverse Sorted Array

Use reverse() after sort():

let arr = ["Apple", "Banana", "Mango"];
arr.sort().reverse();  // ["Mango", "Banana", "Apple"]

🔹 Sorting Arrays of Objects

let users = [
  { name: "Zara", age: 25 },
  { name: "Alex", age: 22 },
  { name: "John", age: 30 }
];

// Sort by age (ascending)
users.sort((a, b) => a.age - b.age);

// Sort by name (alphabetical)
users.sort((a, b) => a.name.localeCompare(b.name));

Practice Questions

Q1. How do you sort an array of fruits alphabetically?

Q2. What happens when you use sort() on numbers without a compare function?

Q3. How do you sort an array of numbers in ascending order?

Q4. Which function is passed to sort() to reverse the order of numbers?

Q5. How do you sort an array of names in descending alphabetical order?

Q6. What method do you use to sort and then reverse the order of the array?

Q7. How can you sort an array of objects by their age property in increasing order?

Q8. Which method helps sort an array of objects alphabetically by name?

Q9. How do you sort the array [100, 2, 50, 10] in descending order correctly?

Q10. What is the output of ["z", "a", "m"].sort()?


JS Array Sort Quiz

Q1: What does ["c", "a", "b"].sort() return?

A. ["c", "b", "a"]
B. ["a", "b", "c"]
C. ["c", "a", "b"]
D. Error

Q2: Which method is used to sort an array?

A. arr.order()
B. arr.sort()
C. arr.filter()
D. arr.compare()

Q3: What will [10, 2, 5].sort() return (without a compare function)?

A. [2, 5, 10]
B. [10, 2, 5]
C. [10, 5, 2]
D. [10, 2, 5]

Q4: How do you sort numbers in ascending order using sort()?

A. arr.sort()
B. arr.sort((a, b) => a - b)
C. arr.sort(a, b)
D. arr.sortUp()

Q5: Which method is used to reverse a sorted array?

A. flip()
B. reverse()
C. turn()
D. down()

Q6: What does sort() modify?

A. Returns a new array
B. Does nothing
C. Modifies the original array
D. Sorts temporarily

Q7: Which function compares two values in custom sorting?

A. callback()
B. compareFunction
C. filterFunction
D. test()

Q8: Which method is used to sort strings alphabetically?

A. order()
B. sort()
C. arrange()
D. alphabet()

Q9: How do you sort an array of objects by a string property like name?

A. sortByName()
B. sort((a, b) => a.name - b.name)
C. sort((a, b) => a.name.localeCompare(b.name))
D. sort((a, b) => a.name > b.name)

Q10: Which method returns the sorted array in reverse order?

A. sort().reverse()
B. sort().flip()
C. order().reverse()
D. arrange().turn()

Go Back Top