Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. The sort() method in JavaScript sorts the elements of an array in place and returns the sorted array. By default, it sorts the elements as strings in ascending order.

    Example: Sorting Strings

    const fruits = ["Banana", "Orange", "Apple", "Mango"];
    fruits.sort();
    console.log(fruits); // ["Apple", "Banana", "Mango", "Orange"]
    Copied!

    Sorting Numbers

    To sort numbers correctly, you need to provide a compare function:

    const points = [40, 100, 1, 5, 25, 10];
    points.sort((a, b) => a - b);
    console.log(points); // [1, 5, 10, 25, 40, 100]
    Copied!

    The compare function (a, b) => a - b sorts the numbers in ascending order.

    Sorting Objects

    You can also sort an array of objects by a specific property:

    const items = [
    { name: "Edward", value: 21 },
    { name: "Sharpe", value: 37 },
    { name: "And", value: 45 },
    { name: "The", value: -12 },
    { name: "Magnetic", value: 13 },
    { name: "Zeros", value: 37 }
    ];
    items.sort((a, b) => a.value - b.value);
    console.log(items);
    Copied!

    This will sort the objects by their value property.

    Important Considerations

    Feedback
  2. JavaScript Array Sort - W3Schools

    The reverse()method reverses the elements in an array: By combining sort() and reverse(),you can sort an array in descending order:
    Javascript Array Tosorted() Method

    ES2023 added the toSorted()method as a safe wayto sort an array without altering the original array. The difference between toSorted() and sort()is that the first method creates a new array, keeping the original array unchanged, while the last method alt…

    Javascript Array Toreversed() Method

    ES2023 added the toReversed()method as a safe wayto reverse an array without altering the original array. The difference between toReversed() and reverse()is that the first methodcreates a new array, keeping the original array unchanged, whil…

    Numeric Sort

    By default, the sort() function sorts values as strings. This works well for strings ("Apple" comes before "Banana"). If numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1". Because of this, the sort()method will …

    The Compare Function

    The purpose of the compare function is to define an alternative sort order. The compare function should return a negative, zero, or positive value, depending on the arguments: When the sort()function compares two values, it sends the values t…

  3. Array.prototype.sort () - JavaScript | MDN

    Jul 20, 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 …

  4. JavaScript Array sort: Sorting an Array More Effectively

    This tutorial shows you how to use the JavaScript array sort method to sort arrays of numbers, strings, and objects.

  5. Sort An Array Of Arrays In JavaScript - GeeksforGeeks

    Jul 23, 2025 · To sort an array of arrays by a specific column, you need to provide a custom comparator function to sort () that specifies which index in the sub-arrays should be compared.

  6. javascript - Sorting an array of objects by property values - Stack ...

    While it is a bit of an overkill for just sorting a single array, this prototype function allows to sort Javascript arrays by any key, in ascending or descending order, including nested keys, using dot syntax.

    • Reviews: 1
    • JavaScript's sort()method: From basics to custom …

      Mar 7, 2025 · Discover how to sort arrays in JavaScript using sort () and toSorted (), including customization and language-sensitive sorting techniques.

    • How to Sort a JavaScript Array Based on Another Array (Without IDs ...

      Jan 16, 2026 · Sorting arrays is a fundamental task in JavaScript, but what if you need to sort one array based on the order defined by another array —and you don’t have unique IDs to map elements?

    • How to Sort Arrays and Arrays of Objects in JavaScript

      JavaScript offers various methods to sort arrays, allowing you to modify the original array, create a sorted copy, or apply custom sorting logic. This guide explores different techniques for sorting both …

    • JavaScript sort - sorting arrays in JS - ZetCode

      Apr 4, 2025 · JavaScript sort tutorial shows how to sort arrays in JavaScript. The tutorial provides numerous examples to demonstrate array sorting in JS.

    • JavaScript Array Sort: Methods for Ordering Array …

      Aug 21, 2024 · In this comprehensive guide, we'll explore the ins and outs of sorting arrays in JavaScript. We'll cover built-in methods, custom sorting functions, and …