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 225031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:22:25+00:00 2026-05-11T19:22:25+00:00

i found the following solution to add sorting capabilities to jQuery (ref: jQuery sort()

  • 0

i found the following solution to add sorting capabilities to jQuery (ref: jQuery sort()). I’ve altered the solution to sort strings of unknown length. It works nicely, however as you might notice from the function name: it sorts acscending :-).

jQuery.fn.sort = function() {  
    return this.pushStack( [].sort.apply( this, arguments ), []);  
};  

function sortStringAscending(a,b){
    var h1 = a.innerHTML;
    var h2 = b.innerHTML;
    var c = 0;
    var str1 = null,
    var str2 = null;

    while(str1 == str2 && (c < h1.length && c < h2.length)) {
        str1 = h1.substring(c,c+1).toLowerCase().charCodeAt(0);
        str2 = h2.substring(c,c+1).toLowerCase().charCodeAt(0);

        if(str1 > str2) {
            r = 1;
        } else if(str2 > str1) {
            r = -1;
        }

        c += 1;
    }
    return r;  
};  

The function is used like this:

$('ol li').sort(sortStringAscending).appendTo('ol');

Is it possible to alter this code in such a way that the following will become possible?

$('ol li').sort(sortString, 0).appendTo('ol');  //0 for descending
$('ol li').sort(sortString, 1).appendTo('ol');  //1 for ascending
  • 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-11T19:22:25+00:00Added an answer on May 11, 2026 at 7:22 pm

    You are not going to be able to easily get an additional argument into the array sort function that the jQuery.fn.sort uses.

    It will be easier to use two separate functions, one for ascending, and one for descending, but keep the actual comparision in a third function.

    function sortStringAscending(a,b) {
        return sortString(a,b,1);
    };
    function sortStringDescending(a,b) {
        return sortString(a,b,-1);
    };
    function sortString(a,b,direction) {
       var h1 = a.innerHTML.toLowerCase();
       var h2 = b.innerHTML.toLowerCase();
    
       if(h1 > h2) {
          r = 1*direction;
       } else if(h2 > h1) {
          r = -1*direction;
       }
       return r;  
    };
    

    Also note that you can simply compare two strings, there is no need to compare them character by character, unless you want to have unpredictable sort order for items like “abc” and “abcd”.

    Then you should be able to do

    $('ol li').sort(sortStringAscending).appendTo('ol');
    $('ol li').sort(sortStringDescending).appendTo('ol');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This was a whole lot easier than I first thought.… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer It seems that stdin/stdout redirect does not work when starting… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer Check ruby version on this server and the other servers… May 11, 2026 at 11:36 pm

Related Questions

i'm fairly new to NHibernate and although I'm finding tons of infos on NHibernate
I haver a simple radio button list on my page that I render with
I have a query in Delphi using DBExpress TSQLQuery that looks like so ActiveSQL.sql.add('SELECT
I just installed Web Developer Express 2008 and the MVC framework (followed all the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.