1

I have a sort function which was working fine but then I tried to add a toggle bit to it (sort asc/desc) and something broke and I can't figure it out. In the code below sortArray is an array of objects, sortBy is which item in the object to sort by and sortDir is asc or desc. So for example if the user wanted to sort by designers then itemA and itemB would be a.designer and b.designer or as I am using it a[sortBy] b[sortBy]. If I console.log itemA once they are set they look fine, they are exactly what I am expecting.

However, when if I plug this code in:

if (itemA < itemB); //sort string ascending
    console.log("a<b");     
    return -1;
if (itemA > itemB);
    console.log("a>b");
return 1;

all I am getting is a < b

function oSort(sortArray, sortBy, sortDir) {

        //run array sort method for strings
        sortArray.sort(function(a, b) {

            if(sortBy == "itemname" || sortBy == "designer") {

                //set the sort items - this is the key of the objects of the array array{object, object, object}
                if(sortBy == "itemname") { 
                    var itemA = $(a[sortBy]).html().toLowerCase();
                    var itemB = $(b[sortBy]).html().toLowerCase();
                } else {  
                    var itemA=a[sortBy].toLowerCase(), itemB=b[sortBy].toLowerCase();
                }

                if (itemA < itemB); //sort string ascending
                    return -1;
                if (itemA > itemB);
                    return 1;

                return 0 //default return value (no sorting)


            } else { 

                if(sortBy == "priority") { 
                    var itemA = $(a[sortBy]).length;
                    var itemB = $(b[sortBy]).length;
                } else if (sortBy == "livedate") { 
                    var itemA = a[sortBy].replace(/\//g, "");
                    var itemB = b[sortBy].replace(/\//g, "");
                } else if (sortBy == "status") { 
                    var itemA = $(a[sortBy]).val();
                    var itemB = $(b[sortBy]).val();
                }


                if(sortDir == "desc") { 
                    return itemA - itemB;
                } else { 
                    return itemB - itemA; 
                }
            }

        });


        return sortArray;

    }

2 Answers 2

4

You have a syntax error:

replace

if (itemA < itemB);

with

if (itemA < itemB)

Ok, not really a syntax error, but an extra semi-colon.

Sign up to request clarification or add additional context in comments.

1 Comment

that my friend is a runtime logic error, the most feared of all errors. It is equivalent to "if(itemA < itemB) {;}". Nice catch. :P
0

look up .reverse() for arrays in js. Not that that solves the issue, but it could solve your need.

1 Comment

Thanks Joseph, yeah, the full code had that I just stripped it out as I was trying to debug my hackery...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.