-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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.
array.sort([compareFunction])
Without compareFunction: sorts elements as strings
With compareFunction: you define how elements should be compared
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)
✅ 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]
Use reverse()
after sort()
:
let arr = ["Apple", "Banana", "Mango"];
arr.sort().reverse(); // ["Mango", "Banana", "Apple"]
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));
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()
?
Q1: What does ["c", "a", "b"].sort() return?
Q2: Which method is used to sort an array?
Q3: What will [10, 2, 5].sort() return (without a compare function)?
Q4: How do you sort numbers in ascending order using sort()?
Q5: Which method is used to reverse a sorted array?
Q6: What does sort() modify?
Q7: Which function compares two values in custom sorting?
Q8: Which method is used to sort strings alphabetically?
Q9: How do you sort an array of objects by a string property like name?
Q10: Which method returns the sorted array in reverse order?