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

  • SEARCH
  • Home
  • 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 a working Browser Helper Object (BHO) which is essentially an ATL based
I have sort of weird situation and I am not sure why it is
For an assignment, we are to write the merge sort function in C: sort(int*
I have this function in charge of making a ajax call, and based on
I have been working in Python for a few months now, and it has
I am working in R. I have a series of coordinates in decimal degrees,
I am working on a Wordpress site that displays a number of Projects stored
I'm working with a PHP script that allows the chronological browsing of Eggdrop-generated IRC
Sorting is giving me a hard time. I need help with an small example,
I've hit a roadblock and cannot for the life of me get over. I

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.