Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:53:17+00:00 2026-05-23T23:53:17+00:00

I have a sort function which was working fine but then I tried to

  • 0

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;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:53:18+00:00Added an answer on May 23, 2026 at 11:53 pm

    You have a syntax error:

    replace

    if (itemA < itemB);
    

    with

    if (itemA < itemB)
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some sort of recursive function, but I need to parse a string,
I have this function in javascript file which works perfectly fine in Firefox /
In .NET, the Generics Lists have a sort function that accepts IComparer or Comparison
I have to sort a number of integers, which can have values between 30.000.000
I have a sort of plug-in model in which various complex user controls are
I have this working function to order a list by the span content var
I'm working on a Javascript/jQuery powered image preloader, and have hit a bit of
I have an input text which is used to filter and sort the contents
I have a working Browser Helper Object (BHO) which is essentially an ATL based
In the primary application on which I have been working, and with which Stack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.