18

Possible Duplicate:
Sort element by numerical value of data attribute

I want to reorganize divs according to a data attribute set on them, ranging from highest integer to lowest. Right now, I push all the data into an array and sort that but I'm not sure how to proceed. How can I use this to reorganize divs inside their parent div?

var prices = new Array();
$(this).children('.listing-item').each(function() {
    var price = parseInt($(this).attr('data-listing-price'));
    prices.push(price)
});
prices.sort(function(a,b){return b-a});

This gets me an array like 139,129,129,109,109,84 for example. Now my idea was to loop through the divs another time with a selector like this:

$('.listing-item[data-listing-price="' + prices[0] + '"]')

but I'm not sure how to move the divs (the div with the highest data-attribute integer should move to the top and so on. Any idea?

1
  • Well... you're right haha. Thanks a lot and sorry I didn't see that one before I posted mine. Commented Jan 21, 2013 at 12:30

3 Answers 3

45

Here is the solution

HTML:

<div id="list">
  <div class="listing-item" data-listing-price="2">2</div>
  <div class="listing-item" data-listing-price="3">3</div>
  <div class="listing-item" data-listing-price="1">1</div>
  <div class="listing-item" data-listing-price="4">4</div>
</div>

JS:

var divList = $(".listing-item");
divList.sort(function(a, b){
    return $(a).data("listing-price")-$(b).data("listing-price")
});
$("#list").html(divList);

JSFiddle:

http://jsfiddle.net/bittu4u4ever/ezYJh/1/

Sign up to request clarification or add additional context in comments.

10 Comments

The callback should return a negative number, positive number or 0, not a boolean.
Updated. But both ways work... ;)
Returning a boolean only seems to work, depending on the data (and algorithm) you can get unexpected results (because you make a to be always equal or larger than b, but never smaller). Read more about Array.sort.
Thanks, this works fine! As Felix said this question is a bit of a duplicate but this answer still helps more than using the answer from the other question.
Oh btw, you should not use .html. I hope jQuery detects this, but in the worst case it serializes all DOM element to HTML and converts them back to DOM elements. You will loose all data and event handlers bound to the elements. Just use .append instead.
|
0
var s = new Array;
var i = 0;
var x = $(".ch").length;
$(".ch").each( function() {
    s[i] = $(this).data("id");
    i++;
});
var g = s.sort(function(a,b){return a-b});
for(var c = 0; c < x; c++) {
    var div = g[c];
    var d = $(".ch[data-id="+div+"]").clone();
    var s = $(".ch[data-id="+div+"]").remove();
    $(".pa").append(d);
}

Working Fiddle

Comments

-1

You can adapt the function described here: How may I sort a list alphabetically using jQuery? and just change the sort function ('<')

<div id="foo">
    <div class="test" data-listing-price="30">30</div>
    <div class="test" data-listing-price="62">62</div>
    <div class="test" data-listing-price="11">11</div>
    <div class="test" data-listing-price="43">43</div>
</div>

In your js (highest integer to lowest):

var div = $('#foo');
var listitems = div.children('div.test').get();
listitems.sort(function (a, b) {
 return (+$(a).attr('data-listing-price') > +$(b).attr('data-listing-price')) ?
  -1 : (+$(a).attr('data-listing-price') < +$(b).attr('data-listing-price')) ? 
   1 : 0;
})
$.each(listitems, function (idx, itm) { div.append(itm); });

http://jsfiddle.net/NfVxk/

4 Comments

The callback should return a negative number, positive number or 0, not a boolean. And neither a nor b have a data-listing-price property. That's not how data attributes work. You have to access them via the .dataset property or use getAttribute (or jQuery of course).
Using < is still wrong, read more about Array.sort.
a nor b yes but $(a) and $(b) have a data-listing-price attribute
> is wrong too... you have to return a positive or negative number or 0, not a boolean. That's why Joseph is using - in the original answer. And honestly I don't see a point in duplicate his answer. I hope you voted to close this question as duplicate at least.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.