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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:54:03+00:00 2026-05-12T15:54:03+00:00

I have a a and I would like to sort my list alphabetically (I

  • 0

I have a a and I would like to sort my list alphabetically (I don’t want caps to matter) according to a class named “name”. How would I do this?

<ul class="column">
  <li>
    <table>
      <tr>
        <td class="name" >Name of Item</td>
      </tr>
      <tr>
        <td>Content</td>
      </tr>
      <tr>
        <td>morecontent</td>
        <td>morecontent</td>
      </tr>
    </table>
  </li>
  <li>
    <table>
      <tr>
        <td class="name" >Another name of item</td>
      </tr>
      <tr>
        <td>Content</td>
      </tr>
      <tr>
        <td>morecontent</td>
        <td>morecontent</td>
      </tr>
    </table>
  </li>
</ul>

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-05-12T15:54:03+00:00Added an answer on May 12, 2026 at 3:54 pm

    Using jQuery, this should do it:

    function sort() {
        $($('ul.column>li').get().reverse()).each(function(outer) {
            var sorting = this;
            $($('ul.column>li').get().reverse()).each(function(inner) {
                if($('td.name', this).text().localeCompare($('td.name', sorting).text()) > 0) {
                    this.parentNode.insertBefore(sorting.parentNode.removeChild(sorting), this);
                }
            });
        });
    }
    

    The above is a little dense though, so if you want to understand what’s going on, let’s break it down line-by-line:

    function sort() {
    
        //get each <li> which is a child of <ul class="column">
        //for each element in the results, execute a function
        //also, we reversed the order (e.g. start at the bottom and go up
        $($('ul.column>li').get().reverse()).each(function(outer) {
    
            //this is the current <li> we're running against
            var sorting = this;
    
            //get the same set of elements again in their current state,
            //so we can figure out where to put this one
            $($('ul.column>li').get().reverse()).each(function(inner) {
    
                //get the inner text of the <td class="name">
                //for the item we're trying to replace,
                //and for the current item in the inner loop
                //use localeCompare to compare the two strings alphabetically
                if($('td.name', this).text().localeCompare($('td.name', sorting).text()) > 0) {
    
                    //if the one we're trying to sort goes after the current one
                    //alphabetically, remove it from its current position
                    //and insert it after the current one
                    this.parentNode.insertBefore(sorting.parentNode.removeChild(sorting), this);
                }
            });
        });
    }
    

    We can make it a little more reusable by passing in the selector for the list and the key:

    sort('ul.column>li', 'td.name');
    
    function sort(list, key) {
        $($(list).get().reverse()).each(function(outer) {
            var sorting = this;
            $($(list).get().reverse()).each(function(inner) {
                if($(key, this).text().localeCompare($(key, sorting).text()) > 0) {
                    this.parentNode.insertBefore(sorting.parentNode.removeChild(sorting), this);
                }
            });
        });
    }
    

    Do keep in mind this requires jQuery, so you’ll need a reference to it in your <head>:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    

    And this function should be called at some point in the page after the list is written in the HTML.

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

Sidebar

Related Questions

If I have a list of elements I would like to sort, Java offers
I have a list of lists that I would like to sort. foreach (var
I have the following list: l = ['50%','12.5%','6.25%','25%'] Which I would like to sort
I have a nested list, List<List<String>> intable; where I would like to sort all
I have a list of numbers which total 540000. I would like to sort
I have the following DOM structure (unordered list), that I would like to sort
I have a problem. I would like to be able to sort the list.
I have a List<Size> list1 which is from parameters.getSupportedPreviewSizes() I would like to sort
I would like to sort a list on base of multiple criteria. public class
I have a list that I would like to sort in multiple ways. My

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.