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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:43:34+00:00 2026-05-22T16:43:34+00:00

I have child divs that I’m trying to sort based on a jquery .data()

  • 0

I have child divs that I’m trying to sort based on a jquery .data() value that I give them that is just a single number. This code works perfectly, but only once, after that I can’t figure out how the heck it’s sorting them. Here is a simplified version:

var myArray = $('#container div').get(); 
myArray.sort(function(x,y) {
return $(x).data('order') - $(y).data('order');
});
$('#container').empty().append(myArray);

I’ve tried so many other different methods of sorting, other plugins, etc., and I can’t get anything to work right. This is as close as I can get. I just have this running on a jquery change event.

Here is the whole thing in case I’m doing something stupid elsewhere:

$('#attorneyFilter').change(function() {
//get array of links for sorting
var myArray = $('#attorneyBlocks div').get(); 
var selectedArea = $(this).val();
//sort alphabetically when "all" is selected
if (selectedArea == 'all') {
    $('#attorneyBlocks div').show();
    myArray.sort(function(a,b) {
        return $(a).text() > $(b).text() ? 1 : -1;
    });
//filter attorneys based on practice area and then assign its order# to the div with data, getting all values from the div's class
} else {
    $('#attorneyBlocks div').hide().each(function() {
        var attorneyArea = $(this).attr('class').split(', ');
        for (var i=0;i<attorneyArea.length;i++) {
            var practiceArea = attorneyArea[i].split('-');
            if (selectedArea == practiceArea[0]) {
                $(this).show().data('order',practiceArea[1]);
            }
        }
    });
    //sort based on order, the lower the number the higher it shows up
    myArray.sort(function(x,y) {
        return $(x).data('order') - $(y).data('order');
    });
}
//append order back in
$('#attorneyBlocks').empty().append(myArray);
});

And a link to the page in question

  • 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-22T16:43:34+00:00Added an answer on May 22, 2026 at 4:43 pm

    Here’s a jsFiddle with this working using .detach() instead of .empty() to keep the data.

    http://jsfiddle.net/shaneblake/Tn9u8/

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

Sidebar

Related Questions

I trying to have a div with child divs that form 2 or more
I have a DIV that has plenty of child DIVs inside of it. What
I have a parent div that is filled with multiple child divs (see here
I have div that contains 2 divs in it. One of the child divs
I have a container div that holds many child divs. One of the divs
I have a div that has a bunch of child divs. I would like
So here is the issue, I have 6 divs that are using jQuery UI
I have a DIV that contains many child elements like e.g. other DIVs, SPANs
As a web developer I frequently will have two floated (child) divs inside of
I have a child form that is throwing an ApplicationException in the Load event

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.