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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:49:09+00:00 2026-05-26T07:49:09+00:00

I’m trying to sort some DIVS in different ways and I’m quite lost. I’ve

  • 0

I’m trying to sort some DIVS in different ways and I’m quite lost. I’ve been trying some stuff but I don’t see how to get it to work.

I have a set of different divs with the same class, different ids, rels and data-attribute inside a container.

<div id="container">
    <div class="sortable" id="div541" rel="1234" data-rel="Name One">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="div354" rel="4321" data-rel="Name Two">
        [some inside html including a table and divs]
    </div>
    <div class="sortable" id="div763" rel="112233" data-rel="Name Three">
        [some inside html including a table and divs]
    </div>
</div>

Then I have a <select> element in which the user select the sort type. Options are by numeric value “stored” in the rel attribute (ascending and descending) and by name “stored” in the data-rel attribute (ascending and descending as well).

First I thought of a jQuery plug-in like Quicksand but it clones the elements changing its ID and I have several elements inside each sortable div which I need to keep their ID to interact with them from outside the container. I searched other plug-ins but they all did some sorting but in a different way and I decided doing this myself.

So I did some googling and stackoverflowing and found that I can sort the divs by doing something like this:

 var ids = ['#div541', '#div354', '#541' ...];
 var cont = $('#resultados');
 $.each(ids, function(i, v) {
     cont.append($(v));
 });

But I have to sort the IDs array before doing the actual sorting and that’s where I’m lost.
Before I go on, is this the best way of achieving the sorting? Will the container.append part modify any ID or duplicate and div?

If it’s okay, can you please help me do it?

I would first do a $('.sortable).each() in wich I make a 3 “column” array. So then I sort the rel or the data-rel attribute ASC or DESC and once the array is sorted I use it to append() the divs back into the container. Is this okay? I hate using arrays specially in JavaScript, so could you please give me a hand to make a function to sort it and then get the list of IDs back in order?

Thanks a lot!

  • 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-26T07:49:09+00:00Added an answer on May 26, 2026 at 7:49 am

    An interesting method to sort would be to create an array of crafted strings that contain the sorted attribute value, a sperarator and the element’s ID.

    In your example data the string array should look like:

    ['Name One#div541', 'Name Two#div354', 'Name Three#div763', ...]
    

    or if you want to sort by the numeric value:

    ['0001234#div541', '0004321#div354', '0112233#div763', ...]
    

    Note that in that case you should pad the number strings with zeros so the sort by string will be correct.

    After that you can simply use the javascript .sort() method on the array. After the array is sorted you can take the IDs by parsing out the data before the delimiter.

    If you want the sorting to be in descending order, just use .reverse() after sorting.

    Edit

    You can convert the crafted array to your desired IDs array like this:

    var idsArray = $.map( craftedArray, function(val, i) {
      return val.substring(val.indexOf('#'));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

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.