Link to home
Start Free TrialLog in
Avatar of ilaird
ilaird

asked on

Multi Level Sorting

This is not a question. This is a gimmy for all you webdevers out there who tend to google for a quick fix before re-inventing the weel. I searched high and low for any example of a multi level sorting function. i didnt care if it was in c++ or perl. Heck i would have even taken sudo code!!  But, after a long invalved dance with web searches and anoyed beond description at the count of "by your code here" sites. i wrote my own multi level sort function. ya ya, not a big deal but i hope, just maby, i will save some one some where the same headach i had.  This is very crude but its a logic example Thanks Experts-Exchange.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
        <script language="javascript">
        var outIndexes;
        var dataSet = new Array(
            {an:"Ian",pc:"PS2G",cap:"2"},
            {an:"Bob",pc:"RR8G",cap:"1.5"},
            {an:"Alice",pc:"PS2G",cap:"1.8"},
            {an:"Dave",pc:"RR8G",cap:"2.51"},
            {an:"Joe",pc:"PS2G",cap:"2.5"},
            {an:"Bill",pc:"RR8G",cap:"2.5"},
            {an:"John",pc:"PS2G",cap:"1.25"},
            {an:"Keven",pc:"PS2G",cap:"1.8"},
            {an:"Rob",pc:"PS2G",cap:"1.95"},
            {an:"Susie",pc:"RR8G",cap:"1.89"}
        );
        
        function displayDataSet(arr)
        {
            PlaceHolder.innerHTML = "";
            var nTable = document.createElement("table");
            nTable.className = "DataTable";
            nTable.cellPadding = 
            nTable.cellSpacing = 0;
            
            var nTH = nTable.insertRow();
            nTH.className = "tableHead";
            for(s in arr[0])
            {
                var nTD = nTH.insertCell();
                nTD.innerHTML = s;
            }
            
            for(var i = 0; i < arr.length; i++)
            {
                var nTR = nTable.insertRow();
                if((i%2) == 0)
                    nTR.className = "tableAlt";
                for(s in arr[i])
                {
                    var nTD = nTR.insertCell();
                    nTD.innerHTML = arr[i][s];
                }
            }
            
            PlaceHolder.appendChild(nTable);
        }
        
        function multiSort(arr, cols)
        {   
            var allArr = new Array();
            allArr.push(arr);
            for(var i = 0; i < cols.length; i++)
            {
                var aBuilder = new Array();
                for(var ai = 0; ai < allArr.length; ai++)
                {
                    allArr[ai] = bubbleSort(allArr[ai], cols[i]);
                    aBuilder = aBuilder.concat(getDupArrays(allArr[ai], cols[i]));
                }
                
                allArr = aBuilder;
            }
            
            var outArr = new Array();
            for( var i = 0; i < allArr.length; i++)
            {
                for( var i2 = 0; i2 < allArr[i].length; i2++)
                {
                    outArr = outArr.concat(allArr[i][i2]);
                }
            }
            
            displayDataSet(outArr);
        }
        
        function getDupArrays(arr, field)
        {
            var rtn = new Array();
            var base = new Array();
            base.push(arr[0]);
            rtn.push(base);
            
            var cSubIndex = 0;
                
            for(var i = 1; i < arr.length; i++)
            {
                if(rtn[rtn.length - 1][0][field] == arr[i][field])
                {
                    rtn[rtn.length - 1].push(arr[i]);
                }
                else
                {
                    rtn.push(new Array());
                    rtn[rtn.length - 1].push(arr[i]);
                }
            }
            
            return rtn;
        }
        
        function bubbleSort(arr, field)
        {
            var temp;
            for(var i = arr.length - 1; i > 0; i--)
            {
                for(var j = 0; j < i; j++)
                {
                    if(arr[j][field] > arr[j+1][field])
                    {
                        temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
            
            return arr;
        }
        
    </script>
    <style>
        .DataTable
        {
            border:solid 1px black;
            width: 500px;
        }
        
        .DataTable td
        {
            padding-left: 5px;
            border-bottom: solid 1px #E1E1E1;
        }
        
        .DataTable .tableHead td
        {
            background-color: #0099CC;
            color: white;
            font-weight: bold;
            border-bottom:solid 1px black;
        }
        
        .DataTable .tableAlt
        {
            background-color: #F1F1F1;
        }
    </style>
</head>
<body onload="displayDataSet(dataSet);">
    
    <div id="PlaceHolder"></div>
    <input type="button" onclick="multiSort(dataSet,new Array('cap','pc','an'));" value="Execute Sort" />
</body>
</html>

Open in new window

Avatar of Badotz
Badotz
Flag of United States of America image

Wow, this sure doesn't work in Firefox 2.0.0.11 :-(
This example is missing CSS declarations, and there is invalid javascript syntax - still, interesting concept...
Avatar of ilaird
ilaird

ASKER

ya, as i said its crude. but i wanted to at least get the logic out there. As for firefox, sadly, i didnt get the chance to test it. if anyone feels like makein some tweeks, i do have 20 points on this post i have to give to someone. Feel free. Thanks all.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial