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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:36:07+00:00 2026-06-17T21:36:07+00:00

Possible Duplicate: How may I sort a list alphabetically using jQuery? I’m using jQuery

  • 0

Possible Duplicate:
How may I sort a list alphabetically using jQuery?

I’m using jQuery to come to the rescue for a limited CMS setup.

I need to sort an ordered list alphabetically, but then I also need to be able to place “special” list items at the top of the list and sort again only those special items descending by their “optional” numerical value.

For example if this was my original markup:

<ul>
    <li class=" ">C</li>
    <li class=" ">A</li>
    <li class=" ">B</li>
    <li class=" ">E</li>
    <li class=" ">D</li>
    <li class="3">Z</li>
    <li class="6">Y</li>
    <li class="14">X</li>
</ul>

… I would want jQuery to sort it so this would be the resulting DOM content:

<ul>
    <li class="14">X</li>
    <li class="6">Y</li>
    <li class="3">Z</li>
    <li class=" ">A</li>
    <li class=" ">B</li>
    <li class=" ">C</li>
    <li class=" ">D</li>
    <li class=" ">E</li>
</ul>

If sorting by a class value is not possible or is overly convoluted, perhaps I could use the following markup structure instead:

<ul>
    <li><span class="numerical">3</span><span class="alphabetical">B</span></li>
</ul>

Does anyone know what jQuery code would allow me to do this? Would I need to run a series of nested “each” actions and “insertAfter” and “insertBefore” functions that somehow compare values? I would think there is a better way, but I’m not sure where to begin my research.

I’m open to any and all suggestions. I’ll also be trying to figure this out on my own in the meantime, but I’m still a jQuery rookie so I doubt I’ll find the right code on my own 🙂

Thanks!!

  • 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-06-17T21:36:08+00:00Added an answer on June 17, 2026 at 9:36 pm

    You need a comparator function, and a way to rearrange the elements. Plugins are available which given the former will do the latter for you, although if the elements are the sole children of a single parent it’s pretty easy (see below).

    For your comparator, you’ll need something which can be given a pair of your <li> elements, and tell you which comes first:

    function myCompare(a, b) {
        var cls_a = parseInt(a.classList, 10);
        var cls_b = parseInt(b.classList, 10);
    
        cls_a = cls_a || -1 ;   // make blank class == -1
        cls_b = cls_b || -1 ;   //   ditto
    
        if (cls_a === cls_b) {
            var str_a = a.innerText || a.textContent;
            var str_b = b.innerText || b.textContent;
            return str_a.localeCompare(str_b);
        } else {
            return cls_a - cls_b;  // numeric descending sort
        }
    }
    

    and for rearranging:

    var $el = $('li');
    var $ul = $('ul');
    $el.sort(myCompare);  // from above
    $el.each(function() {
        $ul.append(this);
    });
    

    The .each loop works by just taking each sorted item, and adding it to the end of the <ul> – when there’s none left, it’s done.

    Shorter versions of the above are possible, e.g.:

    $('li').sort(myCompare).appendTo($('ul'));
    

    See this jsfiddle

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

Sidebar

Related Questions

Possible Duplicate: How may I sort a list alphabetically using jQuery? Technology: Asp.net with
Possible Duplicate: strange while statement behaviour? Given the following, how may I implement list
Possible Duplicate: Pure virtual functions may not have an inline definition. Why? I've come
Possible Duplicate: How do I sort a list of strings in Python? I wrote
Possible Duplicate: C++ using this pointer in constructors Like the title, may I do
Possible Duplicate: load content from external page into another page using ajax/query May be
Possible Duplicate: Jquery event chaining Traditionally we may write: $(selector).click(function () { }); but
Possible Duplicate: Android multiple email attachments using Intent I already know there are may
Possible Duplicate: changing the img src with jquery The title may make this sound
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP

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.