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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:12:31+00:00 2026-05-22T18:12:31+00:00

This function seems to work fairly well for sorting divs based on ID: JS

  • 0

This function seems to work fairly well for sorting divs based on ID:

JS

var div = $("<div id='3.5'>AA</div>");
div_id_after = Math.floor(parseFloat(div.get(0).id));

$('#'+div_id_after).after(div);

HTML

<div id='1'>a</div>
<div id='2'>b</div>
<div id='3'>c</div>
<div id='4'>d</div>
<div id='5'>e</div>

Produces:

a
b
c
AA
d
e

But what if I would like to use a custom attribute titled “order”?

The ID should not be a number for compatibility issues, but does this same rule apply to custom attributes?

  • 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-22T18:12:32+00:00Added an answer on May 22, 2026 at 6:12 pm

    If you’re trying to sort on a custom data attribute, here’s what I’ve done in the past:

    html:

    <ul class="sortList">
        <li data-sort="1">I am number one</li>
        <li data-sort="7">I am number seven</li>
        <li data-sort="22">I am number twenty two</li>
        <li data-sort="2">I am number two</li>
    </ul>
    

    Because the list isn’t in order and it’s not sequential, the easiest way to do the sort is to use javascript’s sort method on a jQuery selected array:

    javascript:

    var list = $('.sortList');
    var listItems = list.find('li').sort(function(a,b){ return $(a).attr('data-sort') - $(b).attr('data-sort'); });
    list.find('li').remove();
    list.append(listItems);
    

    Because jQuery returns an array of elements, the native sort method will give you a sorted array of selectors that you can just replace the contents of the list with.

    After the sort, your list will look like this:

    <ul class="sortList">
        <li data-sort="1">I am number one</li>
        <li data-sort="2">I am number two</li>
        <li data-sort="7">I am number seven</li>
        <li data-sort="22">I am number twenty two</li>
    </ul>
    

    One thing to note as well: I use data-sort as the attribute, because attributes that start with “data-” are treated differently by browsers, so this won’t cause validation issues.

    /////// EDIT:

    Given your comment, here’s another way to do it without replacing the entire array. This will almost certainly be slower and require refining, I would still recommend using the above solution, but if you wanted to append without modifying the list:

    //// This would happen inside a function somewhere
    var list = $('ul li');
    var newItem = $('<li data-sort="7"></li>'); // I assume you will be getting this from somewhere
    $.each(list,function(index,item){
        var prev = parseFloat($(item).attr('data-sort'));
        var next = parseFloat($(list[index+1]).attr('data-sort'));
        var current = parseFloat(newItem.attr('data-sort'));
        if(prev <= current && (next > current || isNaN(next)) ){
            $(item).after(newItem);
        } else if(current > prev){
            $(item).before(newItem);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This doesn't seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m)
I've made an image upload script using the move_uploaded_file function. This function seems to
So there seems to be this problem with GNU Make's $(wildcard) function keeping a
It seems that i cannot access $(this) inside jquery ajax success function. please see
I did this tests and the results seems the count function scale linearly. I
This seems like a fairly simple problem to me but I have been having
I cannot get the following function to work properly. It seems to be serializing
This function appears to be a way to access all sorts of system values.
This function declaration gives me errors: ostream& operator<<(ostream& os, hand& obj); The errors are:
This function is written in ActionScirpt. What kind of decryption this is? Is there

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.