Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one sort a multi dimensional array by multiple columns in JavaScript?
    text
    copied!<p>I've been working on this problem all day without a good solution. Google has been little help as well. I have a script that needs to accept a two dimensional array with an unknown number number of rows/columns. The script also needs to accept a one dimensional array containing a list of columns to sort by, and another containing the order to sort by. The call will look a little like this:</p> <pre><code>var orderList = {0,4,3,1}; var orderDir = {asc,desc,desc,asc}; dataArr = do2DArraySort(dataArr, orderList, orderDir); </code></pre> <p>The function do2DArraySort should return the dataArr array sorted by the first column (in ascending order), then by the fifth (in descending order), then by the third (in descending order), then by the second (in descending order). I was able to make it two levels deep using the code below, but it fell apart once I tried adding a third sort column. I understand why, but I can't figure out a good way to make it work.</p> <p>Is there a standard way of doing this? Could someone point me to a good script online I can study and use as a template? Or can someone suggest a modification to my code to make it work?</p> <p>Thanks!</p> <pre><code>//appends an array content to the original array function addToArray(originalArray, addArray) { if (addArray.length != 0) { var curLength = 0; curLength = originalArray.length; var maxLength = 0; maxLength = curLength + addArray.length; var itrerateArray = 0; for (var r = curLength; r &lt; maxLength; r++) { originalArray[r] = addArray[itrerateArray]; itrerateArray++; } } } function do2DArraySort(arrayToBeSorted, sortColumnArray, sortDirectionArray) { if (arrayToBeSorted == "undefined" || arrayToBeSorted == "null") return arrayToBeSorted; if (arrayToBeSorted.length == 0) return arrayToBeSorted; if (sortColumnArray.length == 0) return arrayToBeSorted; tempArray = arrayToBeSorted; var totalLength = sortColumnArray.length; for(var m = 0; m &lt; totalLength; m++) { if (m == 0) { doBubbleSort(tempArray, tempArray.length, sortColumnArray[m], sortDirectionArray[m]); } else { doMultipleSort(tempArray, sortColumnArray[m], sortColumnArray[m-1], sortDirectionArray[m]); } } return tempArray; } //check if a value exists in a single dimensional array function checkIfExists(arrayToSearch, valueToSearch) { if (arrayToSearch == "undefined" || arrayToSearch == "null") return false; if (arrayToSearch.length == 0) return false; for (var k = 0; k &lt; arrayToSearch.length; k++) { if (arrayToSearch[k] == valueToSearch) return true; } return false; } //sorts an 2D array based on the distinct values of the previous column function doMultipleSort(sortedArray, currentCol, prevCol, sortDirection) { var resultArray = new Array(); var newdistinctValuesArray = new Array(); //finding distinct previous column values for (var n = 0; n &lt; sortedArray.length; n++) { if (checkIfExists(newdistinctValuesArray, sortedArray[n][prevCol]) == false) newdistinctValuesArray.push(sortedArray[n][prevCol]); } var recCursor = 0; var newTempArray = new Array(); var toStoreArray = 0; //for each of the distinct values for (var x = 0; x &lt; newdistinctValuesArray.length; x++) { toStoreArray = 0; newTempArray = new Array(); //find the rows with the same previous column value for (var y = 0; y &lt; sortedArray.length; y++) { if (sortedArray[y][prevCol] == newdistinctValuesArray[x]) { newTempArray[toStoreArray] = sortedArray[y]; toStoreArray++; } } //sort the row based on the current column doBubbleSort(newTempArray, newTempArray.length, currentCol, sortDirection); //append it to the result array addToArray(resultArray, newTempArray); } tempArray = resultArray; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload