How to sort an array on multiple columns using JavaScript ? Last Updated : 25 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The task is to sort the JavaScript array on multiple columns with the help of JavaScript. There two approaches aree two approaches that are discussed below. Approach 1:Combine the multiple sort operations by OR operator and comparing the strings. For comparing the string, we will use the localeCompare() method. Example: This example implements the above approach. First sorting on four columns and then on the two columns. html <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>How to sort an array on multiple columns using JavaScript?</title> </head> <body style="text-align:center;"> <h1>GeeksForGeeks</h1> <p id="GFG_UP"></p> <button onclick="GFG_Fun();">Click Here</button> <p id="GFG_DOWN"></p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var arr = [ [1, 'GFG', 2, 'Geek'], [3, 'g', 1, 'for'], [2, 'portal', 0, 'Geeks'], ]; up.innerHTML = "Click on the button to sort " + "the array on multiple columns based on " + "strings.<br> [" + arr[0] + "] <br> [" + arr[1] + "] <br> [" + arr[2] + "]"; function GFG_Fun() { arr.sort(function (a, b) { return a[3].localeCompare(b[3]) || a[1].localeCompare(b[1]); }); down.innerHTML = "[" + arr[0] + "]<br>[" + arr[1] + "]<br>[" + arr[2] + "]"; } </script> </body> </html> Output: Approach 2: If we sorting the elements on numbers then use the Comparison operators to compare the values. The idea is to first perform the operation on a single column and if the values are similar then move to the next column and do the same.Example: This example implements the above approach. First sorting on three columns and then on one column. html <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>How to sort an array on multiple columns using JavaScript?</title> </head> <body style="text-align:center;"> <h1>GeeksForGeeks</h1> <p id="GFG_UP"></p> <button onclick="GFG_Fun();">Click Here</button> <p id="GFG_DOWN"></p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var arr = [ [1, 'GFG', 2, 'Geek'], [3, 'g', 0, 'for'], [2, 'portal', 0, 'Geeks'], ]; up.innerHTML = "Click on the button to sort " + "the array on multiple columns on " + "strings.<br> [" + arr[0] + "] <br> [" + arr[1] + "] <br> [" + arr[2] + "]"; function GFG_Fun() { arr.sort(function (a, b) { var o1 = a[2]; var o2 = b[2]; var p1 = a[0]; var p2 = b[0]; if (o1 < o2) return -1; if (o1 > o2) return 1; if (p1 < p2) return -1; if (p1 > p2) return 1; return 0; }); down.innerHTML = "[" + arr[0] + "]<br>[" + arr[1] + "]<br>[" + arr[2] + "]"; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to sort an array of object by two fields in JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Sort a Multidimensional Array in JavaScript by Date ? Sorting a Multidimensional Array by date consists of ordering the inner arrays based on the date values. This needs to be done by converting the date representation into the proper comparable formats and then applying the sorting function to sort the array in ascending or descending order. Below are 2 min read How to sort an array of object by two fields in JavaScript ? We have given an array of objects and the task is to sort the array of elements by 2 fields of the object. There are two methods to solve this problem which are discussed below: Approach 1:First compare the first property, if both are unequal then sort accordingly.If they are equal then do the same 3 min read How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort 3 min read JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana', 3 min read How To Sort An Array Without Mutating The Original Array In JavaScript? Sorting arrays is a common task in JavaScript, especially when you need to present data in a specific order. However, maintaining the original array is often necessary especially if the original data will be required later in your code. In this article we'll see how we can sort an array in JavaScrip 2 min read How to Sort an Array Based on the Length of Each Element in JavaScript? Imagine you have a list of words or groups of items, and you want to arrange them in order from shortest to longest. This is a pretty common task in JavaScript, especially when working with text or collections of things. By sorting your list in this way, you can make sense of your data and make it e 3 min read Like