About 430,000 results
Open links in new tab
  1. JavaScript - Sort JS Array in Ascending Order - GeeksforGeeks

    Nov 14, 2024 · 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 Used. Pass the comparator function in array.sort () to sort the array in ascending order.

  2. javascript - How to sort an array of integers? - Stack Overflow

    Jun 30, 2009 · Ascending order with array.sort(compareFunction): const myArray = [104, 140000, 99]; myArray.sort(function(a, b){ return a - b; }); console.log(myArray); // output is [99, 104, 140000] Explanation from w3schools: compareFunction defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the ...

  3. JavaScript Array Sort - W3Schools

    By combining sort() and reverse(), you can sort an array in descending order: ES2023 added the toSorted() method as a safe way to sort an array without altering the original array.

  4. algorithm - Javascript Array.sort implementation? - Stack Overflow

    Jan 28, 2014 · Numeric arrays (or arrays of primitive type) are sorted using the C++ standard library function std::qsort which implements some variation of quicksort (usually introsort). Contiguous arrays of non-numeric type are stringified and sorted using mergesort, if available (to obtain a stable sorting) or qsort if no merge sort is available.

  5. Sorting Algorithms in JavaScript - GeeksforGeeks

    Feb 12, 2025 · Sorting is an important operation in computer science that arranges elements of an array or list in a certain order (either ascending or descending). Example: Bubble Sort is one of the simplest sorting algorithms. It repeatedly compares adjacent elements and swaps them if they are in the wrong order. Why is Bubble Sort Stable?

  6. Sort an Array in JavaScript - GeeksforGeeks

    Nov 14, 2024 · To sort an array in JavaScript, we can use array.sort () method. This method sorts the elements alphabetically in ascending order. Please refer to the JavaScript Array Tutorial for a comprehensive understanding of JavaScript Arrays. Using Comparator function.

  7. Array.prototype.sort() - JavaScript | MDN - MDN Web Docs

    Apr 3, 2025 · The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their …

  8. JavaScript Sort() – How to Use the Sort Function in JS

    May 16, 2023 · The sort() function allows you to sort an array object by either the default sorting order, or by a custom sorting function. By default, it sorts the elements in the array in ascending order based on their string Unicode values.

  9. JavaScript Array sort: Sorting an Array More Effectively

    By default, the sort() method sorts the array elements in ascending order, placing the smallest value first and the largest value last. When you sort an array of numbers, the sort() method converts these numbers to strings and compares the strings to determine the order.

  10. How can you sort an array without mutating the original array?

    The Array.prototype.toSorted(compareFn) -> Array is a new method that was proposed to be added to the Array.prototype and is currently in stage 3 (Soon to be available). This method will keep the target Array untouched and return a copy with the change performed instead.