JavaScript - Sort JS Array in Ascending Order Last Updated : 14 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Sorting JS Array in ascending order refers to arranging elements from the smallest to largest element. We can use JS array sort, sorting algorithms, and external libraries to sort array. 1. Using JS Array.sort() Method- Mostly UsedPass the comparator function in array.sort() to sort the array in ascending order. JavaScript const arr = [ 10, 20, 25, 100 , 40] arr.sort((a,b) => a - b) console.log(arr) Output[ 10, 20, 25, 40, 100 ] Please go through this, How to Sort Numeric Array using JavaScript?, to know how the javascript array sort function works.2. Sort Array of StringThe string.localeCompare() method is used to compare the strings alphabetically. JavaScript const arr = ['HTML', 'CSS', 'JS']; arr.sort((a, b) => a.localeCompare(b)); console.log(arr); 3. Using Sorting AlgorithmsThe sorting algorithms like bubble sort can be used to sort JS array in Ascending Order. JavaScript const arr = [10, 20, 25, 100, 40]; function bubbleSort(arr) { let n = arr.length; // Traverse through all array elements for (let i = 0; i < n - 1; i++) { for (let j = 0; j < n - 1 - i; j++) { // Swap if element is greater than next index if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; } } } return arr; } console.log(bubbleSort(arr)); Output[ 10, 20, 25, 40, 100 ] Check this article, Sorting Algorithms in JS, to know more about the sorting algorithms in JavaScript 4. Using Lodash.sortBy() methodThe _.sortBy() method is used to sort the given data. It sort the given array in ascending order. JavaScript const _ = require('lodash'); const arr = [10, 20, 25, 100, 40]; _.sortBy(arr); console.log(arr); // [10, 20, 25, 40, 100] Output[10, 20, 25, 40, 100] Comment More infoAdvertise with us Next Article JavaScript - Sort JS Array in Ascending Order G geekwriter2024 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Check an Array is Sorted or Not In this article, we will learn how to check if the array is sorted in JavaScript. JavaScript Array is a single variable that is used to store elements of different data types. Now, we have to find whether the array is sorted or not.Examples:Input : const arr = [1, 2, 3, 4, 5]; Output : trueInput : c 4 min read JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case In this article, we are given an array of strings, you need to sort the given array of strings containing both uppercase and lowercase characters in ascending order by ignoring the case in JavaScript. Example 1: Input :- arr = ["Geeks", "for", "geeks", "is", "The", "Best"] Output :- [ 'Best', 'for', 3 min read JavaScript Program to Sort Words in Alphabetical Order Sorting words in alphabetical order involves arranging them based on the standard sequence of letters in the alphabet. This process helps in organizing data, making searches efficient, and presenting information in a clear and systematic manner.Methods to sort wordsTable of Content Approach 1: Using 5 min read JavaScript Program to Sort an Associative Array by its Values In Javascript, "arrays" are indexed by numerical values, while "objects" are essentially what you might refer to as associative arrays. It's important to note that "objects" do not have a guaranteed order. In this article, we will learn how can we sort an associative array by its values. Table of Co 4 min read JavaScript Program to Reorder an Array According to Given Indexes Reordering an array according to given indexes means rearranging the elements of an array based on a separate array that specifies the desired order. Each element in the given index array corresponds to the index of the element in the original array at which it should be placed in the reordered arra 3 min read Like