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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:57:43+00:00 2026-05-28T02:57:43+00:00

I have a set of three list items that I would like to automatically

  • 0

I have a set of three list items that I would like to automatically display from high to low on page load. Ideally using jquery or javascript.

<ul class="list">
<li id="alpha">32</li>
<li id="beta">170</li>
<li id="delta">28</li>
</ul>

Each list item needs its own ID because they each have individual background images. The numbers must text nodes so that a user can edit them.

  • 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-28T02:57:43+00:00Added an answer on May 28, 2026 at 2:57 am

    This will probably be the fastest way to do it, since it doesn’t use jQuery:

    function sortList(ul){
        var new_ul = ul.cloneNode(false);
    
        // Add all lis to an array
        var lis = [];
        for(var i = ul.childNodes.length; i--;){
            if(ul.childNodes[i].nodeName === 'LI')
                lis.push(ul.childNodes[i]);
        }
    
        // Sort the lis in descending order
        lis.sort(function(a, b){
           return parseInt(b.childNodes[0].data , 10) - 
                  parseInt(a.childNodes[0].data , 10);
        });
    
        // Add them into the ul in order
        for(var i = 0; i < lis.length; i++)
            new_ul.appendChild(lis[i]);
        ul.parentNode.replaceChild(new_ul, ul);
    }
    

    Call the function like:

    sortList(document.getElementsByClassName('list')[0]);
    

    You can sort other lists the same way, and if you have other elements on the same page with the list class you should give your ul an id and pass it in using that instead.

    Example JSFiddle

    Edit

    Since you mentioned that you want it to happen on pageLoad, I’m assuming you want it to happen ASAP after the ul is in the DOM which means you should add the function sortList to the head of your page and use it immediately after your list like this:

    <head>
        ...
        <script type="text/javascript">
            function sortList(ul){
                var new_ul = ul.cloneNode(false);
                var lis = [];
                for(var i = ul.childNodes.length; i--;){
                    if(ul.childNodes[i].nodeName === 'LI')
                        lis.push(ul.childNodes[i]);
                }
                lis.sort(function(a, b){
                   return parseInt(b.childNodes[0].data , 10) - parseInt(a.childNodes[0].data , 10);
                });
                for(var i = 0; i < lis.length; i++)
                    new_ul.appendChild(lis[i]);
                ul.parentNode.replaceChild(new_ul, ul);
            }
        </script>
    </head>
    <body>
        ...
        <ul class="list">
            <li id="alpha">32</li>
            <li id="beta">170</li>
            <li id="delta">28</li>
        </ul>
        <script type="text/javascript">
            !function(){
                var uls = document.getElementsByTagName('ul');
                sortList( uls[uls.length - 1] );
            }();
        </script>
        ...
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to pass a list/set/tuple from python (via psycopg2)
What I would like to happen is to have a menu of list items
I have a set of three vectors: x, y, and z that I want
I have a three tier set-up. Someone suggested I should get the ConnectionString from
I have a table that has three different date columns, so I set each
I have a WPF Menu and a Tab control. I would like the list
I have a simple list-type php page, which lists items according to a mysql
I have a list of items that have a partial order relation , i.
I have items contained in an unordered list. I have hidden divs (using display:none;)
WinForms, .NET 3.5 I have a ComboBox that needs to display a growing list

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.