{"id":11856,"date":"2023-01-19T10:01:20","date_gmt":"2023-01-19T10:01:20","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11856"},"modified":"2023-06-08T13:12:21","modified_gmt":"2023-06-08T13:12:21","slug":"javascript-array-sort-method","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/","title":{"rendered":"JavaScript Array Sort() Method"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg\" alt=\"\" \/><\/p>\n<p>You may arrange array elements in a sequential sequence using the Array sort() method. In addition to returning the sorted array, the Array sort() method changes the positions of the items in the starting array.<\/p>\n<p>The array items are ordinarily sorted using the sort() function in ascending order, with the smallest value coming before the biggest.<\/p>\n<p>In order to establish the ordering, the Array sort() function converts items to strings and compares the strings.<\/p>\n<p><strong>Consider the following example:<\/strong><\/p>\n<pre><code>let numbers = [0, 1 , 2, 3, 10, 20, 30 ];\nnumbers.sort();\nconsole.log(numbers);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[ 0, 1, 10, 2, 20, 3, 30 ]<\/code><\/pre>\n<p>In this case, the string &quot;10&quot; occurs before &quot;2&quot; while comparing strings, therefore the sort() function inserts &quot;10&quot; before &quot;2&quot;.<\/p>\n<p>You must supply a comparison function to the sort() method in order to correct this. The comparative function will be used by the Array sort() method to determine the elemental ordering.<\/p>\n<h2>Syntax of the Array sort() function<\/h2>\n<pre><code>array.sort(comparefunction)<\/code><\/pre>\n<p>A function that compares two array elements is an optional parameter to the Array sort() method.<\/p>\n<p>If the comparison function is not used, the Array sort() method sorts the elements according to the previously given Unicode code point values. <\/p>\n<p>The Array sort() method&#8217;s comparison function takes two inputs and produces a result that indicates the sort order. The comparison function&#8217;s syntax is demonstrated by the following:<\/p>\n<pre><code>function compare(a,b) {\n  \/\/ ...\n}<\/code><\/pre>\n<p>Two inputs, a and b, are accepted by the compare() method. The compare() function&#8217;s return value is used by the sort() method to order the items according to the following guidelines:<\/p>\n<ul>\n<li>The sort() function orders a lower in the index than b if compare(a,b) is less than zero. Meaning that a will come first.<\/li>\n<li>The sort() function sorts b to a lower index than a, meaning b will come first, if compare(a,b) is higher than zero.<\/li>\n<li>The sort() function assumes that a and b are equal and maintains their current places if compare(a,b) returns zero.<\/li>\n<\/ul>\n<p>You may use the following syntax to resolve the number sorting problem:<\/p>\n<pre><code>let numbers = [0, 1 , 2, 3, 10, 20, 30 ];\nnumbers.sort( function( a , b){\n    if(a &gt; b) return 1;\n    if(a <b> {\n    if(a &gt; b) return 1;\n    if(a <b> a - b);\nconsole.log(numbers);<\/code><\/pre>\n<h2>Sorting an Array of Strings<\/h2>\n<p>Let&#8217;s say you have an array of strings called animals that looks like this:<\/p>\n<pre><code>let animals = [\n    'cat', 'dog', 'elephant', 'bee', 'ant'\n];<\/code><\/pre>\n<p>The following example demonstrates how to use the Array sort() method without specifying the comparison function to alphabetically organize the animals array&#8217;s items in ascending order:<\/p>\n<pre><code>let animals = [\n    'cat', 'dog', 'elephant', 'bee', 'ant'\n];\nanimals.sort();\nconsole.log(animals);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[ 'ant', 'bee', 'cat', 'dog', 'elephant' ]<\/code><\/pre>\n<p>You must modify the comparison function&#8217;s logic and feed it to the sort() method as shown in the example below in order to sort the animal&#8217;s array in descending order.<\/p>\n<pre><code>    let animals = [\n    'cat', 'dog', 'elephant', 'bee', 'ant'\n    ];\n\nanimals.sort((a, b) =&gt; {\n    if (a &gt; b)\n        return -1;\n    if (a <b> y ? 1 : -1;\n\n});<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[ 'ant', 'bee', 'Cat', 'dog', 'Elephant' ]<\/code><\/pre>\n<h2>Sorting an Array of Strings with Non-ASCII Characters<\/h2>\n<p>With strings containing ASCII characters, the sort() method functions as intended. However, the sort() method will not function properly for strings that contain non-ASCII characters, such as \u00e9, \u00e8, etc. For instance:<\/p>\n<pre><code>let animaux = ['z\u00e8bre', 'abeille', '\u00e9cureuil', 'chat'];\nanimaux.sort();\n\nconsole.log(animaux);<\/code><\/pre>\n<p>The \u00e9cureuil string, as you can see, ought to come before the z\u00e8bre string.<\/p>\n<p>To fix this, compare strings in a certain locale using the localeCompare() function of the String object, like in the following example:<\/p>\n<pre><code>animaux.sort(function (a, b) {\n    return a.localeCompare(b);\n});\nconsole.log(animaux);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[ 'abeille', 'chat', '\u00e9cureuill', 'z\u00e8bree' ]<\/code><\/pre>\n<p>The elements of the animaux array now are in the correct order.<\/p>\n<h2>Sorting an Array of Numbers<\/h2>\n<p>Assume you have a collection of scores, like in the example below.<\/p>\n<pre><code>let scores = [\n    9, 80, 10, 20, 5, 70\n];<\/code><\/pre>\n<p>You must input a custom comparison function that compares two numbers into an array of numbers in order to numerically sort them.<br \/>\nThe scores array is numerically sorted in ascending order in the example that follows.<\/p>\n<pre><code>let scores = [\n    9, 80, 10, 20, 5, 70\n];\n\/\/ sort numbers in ascending order\nscores.sort((a, b) =&gt; a - b);\n\nconsole.log(scores);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[ 5, 9, 10, 20, 70, 80 ]<\/code><\/pre>\n<p>You just need to use the compare function&#8217;s logic in the other direction, as seen in the example below, to numerically arrange an array of integers in descending order:<\/p>\n<pre><code>let scores = [\n    9, 80, 10, 20, 5, 70\n];\n\/\/ descending order\nscores.sort((a, b) =&gt; b - a);\nconsole.log(scores);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[80, 70, 20, 10, 9, 5]<\/code><\/pre>\n<h2>Sorting an Array of Objects by a Specified Property<\/h2>\n<p>An array of employee objects is shown below. Each object has the three values name, salary, and hireDate.<\/p>\n<pre><code>let employees = [\n    {name: 'John', salary: 90000, hireDate: \"July 1, 2010\"},\n    {name: 'David', salary: 75000, hireDate: \"August 15, 2009\"},\n    {name: 'Ana', salary: 80000, hireDate: \"December 12, 2011\"}\n];<\/code><\/pre>\n<h2>Sorting Objects by a Numeric Property<\/h2>\n<p>The method for sorting the employees&#8217; salaries in ascending order is demonstrated in the example below.<\/p>\n<pre><code>\/\/ sort by salary\nemployees.sort(function (x, y) {\n    return x.salary - y.salary;\n});\n\nconsole.table(employees);<\/code><\/pre>\n<p>This illustration is comparable to the illustration of sorting an array of integers upwards. The distinction is because it contrasts two items salaries as opposed to other properties.<\/p>\n<h2>Sorting Objects by a String Property<\/h2>\n<p>You supply the compare function, which compares two strings case-insensitively, the following information in order to case-insensitively arrange the workers array by name property:<\/p>\n<pre><code>employees.sort(function (x, y) {\n    let a = x.name.toUpperCase(),\n        b = y.name.toUpperCase();\n    return a == b ? 0 : a &gt; b ? 1 : -1;\n});\n\nconsole.table(employees);<\/code><\/pre>\n<h2>Sorting Objects by the Date Property<\/h2>\n<p>Lets say you want to organise the workers by the date they were hired.<\/p>\n<p>The hireDate property of the employee object contains the hiring date information. However, it isn&#8217;t the Date object; rather, it&#8217;s only a string that symbolises a legitimate date.<\/p>\n<p>Therefore, in order to compare two dates, which is equivalent to comparing two integers, you must first generate a valid Date object from the date string.<br \/>\nThe answer is as follows:<\/p>\n<pre><code>employees.sort(function (x, y) {\n    let a = new Date(x.hireDate),\n        b = new Date(y.hireDate);\n    return a - b;\n});\n\nconsole.table(employees);<\/code><\/pre>\n<h2>Optimising JavaScript Array sort() Method<\/h2>\n<p>In actuality, the comparison function is used many times by the sort() method for each entry in the array.<br \/>\nSee the example below:<\/p>\n<pre><code>let rivers = ['Nile', 'Amazon', 'Congo', 'Mississippi', 'Rio-Grande'];\n\nrivers.sort(function (a, b) {\n    console.log(a, b);\n    return a.length - b.length;\n});<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>Amazon Nile\nCongo Amazon\nCongo Amazon\nCongo Nile\nMississippi Congo\nMississippi Amazon\nRio-Grande Amazon\nRio-Grande Mississippi<\/code><\/pre>\n<h2>How it works:<\/h2>\n<ul>\n<li>Declare a list of rivers first, including just the well-known river names.<\/li>\n<li>Second, use the sort() function to order the river array according to the length of each entry. Every time the sort() method uses the comparison function, we report the contents of the rivers array to the browser console.<\/li>\n<li>Each element has been assessed more than once, as seen in the result above, for example, Amazon has been reviewed four times and Congo twice.<\/li>\n<li>Performance might be affected by an increase in the number of array elements.<\/li>\n<li>The number of times the comparison function is used cannot be decreased. You may, however, lessen the amount of effort the comparison needs to perform. This process is known as the Schwartzian Transform.<\/li>\n<\/ul>\n<p><strong>To implement this, you follow these steps:<\/strong><\/p>\n<ol>\n<li>Use the map() function to first extract the real values into a temporary array.<\/li>\n<li>Next, order the temporary array by already-evaluated items (or transformed).<\/li>\n<li>Third, navigate the informal array to obtain an array containing the proper ad hoc.<\/li>\n<\/ol>\n<p><strong>Here is the solution:<\/strong><\/p>\n<pre><code>\/\/ temporary array holds objects with position\n\/\/ and length of element\nvar lengths = rivers.map(function (e, i) {\n    return {index: i, value: e.length };\n});\n\n\/\/ sorting the lengths array containing the lengths of\n\/\/ river names\nlengths.sort(function (a, b) {\n    return +(a.value &gt; b.value) || +(a.value === b.value) - 1;\n});\n\n\/\/ copy element back to the array\nvar sortedRivers = lengths.map(function (e) {\n    return rivers[e.index];\n});\n\nconsole.log(sortedRivers);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[ 'Nile', 'Congo', 'Amazon', 'Rio-Grande', 'Mississippi' ]<\/code><\/pre>\n<p><strong>Conclusion<\/strong><br \/>\nThe JavaScript Array..sort() method is a powerful tool for sorting the elements of an array. It provides flexibility and customization options for sorting arrays of different data types. The method sorts the elements in place and returns the sorted array. It uses a stable sorting algorithm, typically a variant of quicksort or mergesort, to efficiently sort the array.<\/p>\n<h2>Frequently Asked Questions (FAQs):<\/h2>\n<p><strong>Q1: How does the sort() method work in JavaScript?<\/strong><br \/>\nThe sort() method in JavaScript converts the elements of the array into strings and then compares them based on their Unicode code points. By default, the elements are sorted in ascending order. The method modifies the original array and returns the sorted array.<\/p>\n<p><strong>Q2: Can I use the sort() method to sort arrays of different data types?<\/strong><br \/>\nYes, the sort() method can sort arrays containing elements of different data types. However, the sorting order may not always be intuitive. For example, if you have an array of numbers and strings, the numbers will be converted to strings and sorted based on their Unicode code points.<\/p>\n<p><strong>Q3: How can I customize the sorting order using the sort() method?<\/strong><br \/>\nThe sort() method accepts an optional compare function that can be used to define a custom sorting order. The compare function takes two parameters (a and b) representing two elements being compared. It should return a negative value if a should be placed before b, a positive value if a should be placed after b, and 0 if both elements are equal in terms of sorting.<\/p>\n<p><strong>Q4: Can the sort() method handle the sorting of complex objects?<\/strong><br \/>\nYes, the sort() method can sort arrays of complex objects. By providing a compare function that specifies the sorting criteria based on specific object properties, you can customize the sorting order for complex objects. For example, you can sort an array of objects based on their name or age properties.<\/p>\n<p><strong>Q5: What is the time complexity of the sort() method?<\/strong><br \/>\nThe time complexity of the sort() method depends on the sorting algorithm used by the JavaScript engine. In most modern engines, the average time complexity is O(n log n), where n is the number of elements in the array. However, it&#8217;s important to note that the worst-case time complexity can be O(n^2) for some sorting algorithms, depending on the data and the compare function used.<\/p>\n<p><strong>Q6: Does the sort() method modify the original array?<\/strong><br \/>\nYes, the sort() method modifies the original array in place. It rearranges the elements of the array according to the sorting order and returns the sorted array. If you want to preserve the original array, you should create a copy of it before applying the sort() method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may arrange array elements in a sequential sequence using the Array sort() method. In addition to returning the sorted array, the Array sort() method changes the positions of the items in the starting array. The array items are ordinarily sorted using the sort() function in ascending order, with the smallest value coming before the [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[162],"tags":[],"class_list":["post-11856","post","type-post","status-publish","format-standard","hentry","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Array Sort() Method: Definition, Working and Methods.<\/title>\n<meta name=\"description\" content=\"Understanding JavaScript Array sort() Method. Here we will learn about its definition, working and different methods .\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Array Sort() Method: Definition, Working and Methods.\" \/>\n<meta property=\"og:description\" content=\"Understanding JavaScript Array sort() Method. Here we will learn about its definition, working and different methods .\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-19T10:01:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-08T13:12:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"JavaScript Array Sort() Method\",\"datePublished\":\"2023-01-19T10:01:20+00:00\",\"dateModified\":\"2023-06-08T13:12:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/\"},\"wordCount\":1421,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg\",\"articleSection\":[\"Javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/\",\"name\":\"JavaScript Array Sort() Method: Definition, Working and Methods.\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg\",\"datePublished\":\"2023-01-19T10:01:20+00:00\",\"dateModified\":\"2023-06-08T13:12:21+00:00\",\"description\":\"Understanding JavaScript Array sort() Method. Here we will learn about its definition, working and different methods .\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Javascript\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Array Sort() Method\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Array Sort() Method: Definition, Working and Methods.","description":"Understanding JavaScript Array sort() Method. Here we will learn about its definition, working and different methods .","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Array Sort() Method: Definition, Working and Methods.","og_description":"Understanding JavaScript Array sort() Method. Here we will learn about its definition, working and different methods .","og_url":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-01-19T10:01:20+00:00","article_modified_time":"2023-06-08T13:12:21+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"JavaScript Array Sort() Method","datePublished":"2023-01-19T10:01:20+00:00","dateModified":"2023-06-08T13:12:21+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/"},"wordCount":1421,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg","articleSection":["Javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/","url":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/","name":"JavaScript Array Sort() Method: Definition, Working and Methods.","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg","datePublished":"2023-01-19T10:01:20+00:00","dateModified":"2023-06-08T13:12:21+00:00","description":"Understanding JavaScript Array sort() Method. Here we will learn about its definition, working and different methods .","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674107524072-JavaScript%20Array%20Sort%28%29%20Method.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/javascript-array-sort-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Javascript","item":"https:\/\/prepbytes.com\/blog\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Array Sort() Method"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11856"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11856\/revisions"}],"predecessor-version":[{"id":16737,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11856\/revisions\/16737"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}