HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptMinor

How to verify if HTML table sort is stable or not?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
tablestableverifyhowsortnothtml

Problem

I have implemented a HTML table sorting. I actually implemented a sort first using jQuery and then modified it based on the answer to this question for performance as I had done the same mistake. My question is how to make sure if the code I have written is stable or not?

I know that the stability of sorting depends on the sort method's algorithm and that changes with the browser, and according to this page, mozilla's implementation of sorting is stable, but when I checked with my code, it doesn't seem to be stable. Could someone have a look at my code and let me know how to determine whether the sort performed is stable or not?


        
        
        $(document).ready(function(){
        asc = new Array();
        for (var a=0;a" + arr[i].join("") + ""
        }
        $("tbody").html(""+arr.join("")+"");
        asc[c] = (asc[c]==1)?-1:1;
    });});
        
        
        table { border-collapse: collapse; border: none;}
        th,
        td {
            border: 1px solid black;
            padding: 4px 16px;
            font-family: Times New Roman;
            font-size: 24px;
            text-align: left;
        }
        th {
            background-color: #D8D8D8;
            cursor: pointer;
        }
    
    
        
            
                
                    Month
                    Savings
                
            
            
                
                    March
                    180
                
                
                    January
                    100
                
                
                    February
                    80
                
                
                    Januray
                    40
                
                
                    March
                    200
                
                
                    February
                    90
                
            
         
    


And if there can be any improvements for efficiency, can anyone

Solution

From a once over;

  • The easiest way to be sure that you sort algorithm is stable, is to write you own sort algorithm.



  • The second easiest way to be sure is to pass in each array entry object it's position in the array ( you could set this after your arr[i][j] assignment ). If (a[c]==b[c]) then you would not return 0, but you would return -1 or +1 based on their original position in the array



  • You did not declare c and asc with var



  • var arr = []; is preferred to var arr = new Array();



  • One single comma separated var statement is preferred over many var statements



  • You declare var i twice, since var i declares i for the scope of the function, you do not need to declare it again.



  • Your indenting is off, you should look into that



  • Assigning a function to document.ready is old skool, you should look into using addEventListener or simple using $( handler );.



-
You should cache the value of `a

-
On my second point, imagine you are sorting people by their age, in a stable manner:

Bob, 23 years
John, 12 years
Tricia, 23 years

becomes

Bob, 23 years
Tricia, 23 years
John, 12 years

it would be unstable if the order was

Tricia, 23 years
Bob, 23 years
John, 12 years

because Tricia was initially below Bob, she should stay below Bob.
You can accomplish this by comparing the original position of the 2 records and making sure that if the age is the same, you make sure the original order is respected.

I hope this example made it clearer.

Code Snippets

for (var a = 0, count = $("tbody tr:eq(0) td").length; a < count ; a++)

Context

StackExchange Code Review Q#45818, answer score: 3

Revisions (0)

No revisions yet.