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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:09:11+00:00 2026-05-20T05:09:11+00:00

I have a list of DIVs, like this : <div id=list> <div class=classA classB

  • 0

I have a list of DIVs, like this :

<div id="list">
    <div class="classA classB SORT-4.2"><div>4</div></div>
    <div class="classA SORT-3.3"><div>3</div></div>
    <div class="classC classD classE SORT-5.1"><div>5</div></div>
    <div class="classB classC SORT-1.5"><div>1</div></div>
    <div class="classA class B class C SORT-2.4"><div>2</div></div>
</div>

and I want to sort them, using jQuery

Server-side, I can define the wanted order, and include this order in the class names :

SORT-4.2

I want to be able to call either the first order (in my example this DIV will be in the 4th position), or the second order (it will be in the 2nd position) : that explains the “4.2”

So if I call OrderDIV(1) I will have this :

1
2
3
4
5

and if I call OrderDIV(2) I will have this :

5
4
3
2
1

(I will need to add more orders, like : SORT-4.2.5.6.2)

As you can see DIVs can have multiple classes, only the pattern SORT- defines the class used to sort

Thank you VERY MUCH for your help!

  • 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-20T05:09:12+00:00Added an answer on May 20, 2026 at 5:09 am

    Create a function that accepts the divs and the order number (1,2,etc).

    Convert the divs from a jQuery object to an Array of DOM elements.

    Use a regular expression to extract the proper information from the className and overwrite the Array so it looks like:

    arr[0] = {num:4, el:/* the element */}
    arr[1] = {num:2, el:/* the element */}
    arr[2] = {num:3, el:/* the element */}
    

    Then sort on a.num and b.num.

    Finally iterate the array, appending each element.

    function sort( divs, order ) {
        var arr = divs.toArray(),
            len = arr.length,
            parent = divs.parent(),
            i = 0;
    
        while( i < len ) {
            var num = arr[ i ].className.match(/(?:SORT-([\d.]+))/)[1].split('.');
            arr[ i ] = {num:num[ order - 1 ],el:arr[ i ]};
            i++;
        }
        arr.sort(function(a,b) {
            return a.num - b.num;
        });
        i = 0;
        while( i < len ) {
            parent.append( arr[i].el );
            i++;
        }
    }
    

    Result is:

    Example: http://jsfiddle.net/TsArJ/10/

    sort( $('#list > div'), 3 ); // 1,2,3,4,5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of divs (simplified example) <div class=title>text</div> <div class=description>text</div> <div class=title>text</div>
I have a list of 2-item tuples and I'd like to convert them to
I have a list of div pairs: MainDiv's and infPanel Divs. I need to
I have an overview list, with divs, and also each div has a pair,
Story: I have a list of products. I want to be able to sort
I have a HTML structure like follows <div id=answer-1> <a class=edit></a> </div> <div id=answer-2>
Say I have a list of divs identified by class .e.g '.myclass' Inside each
In my app I have 2 divs, one with a long list of products
I have a list of integers, List<Integer> and I'd like to convert all the
I have a List<int> and a List<customObject> . The customObject class has an ID

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.