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

LVL 2
ilairdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BadotzCommented:
Wow, this sure doesn't work in Firefox 2.0.0.11 :-(
0
BadotzCommented:
This example is missing CSS declarations, and there is invalid javascript syntax - still, interesting concept...
0
ilairdAuthor Commented:
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.
0
Computer101Commented:
PAQed with points refunded (20)

Computer101
EE Admin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.