This question is related to this question, but I have another issue which I need some help with. I would like to be able to group my divs into two groups - both sorted. The sorting already works, but how do I seperate my dovs up into two groups? The container divs in group 2 contains an element with class="anotherGroup".
the markup is this:
<div class="container">
<div class="terminal" data-price="195">
<h3>195</h3>
</div>
195 - I should come in 2nd in group 1 which is displayed first.
</div>
<div class="container">
<div class="terminal" data-price="220">
<h3>220</h3>
</div>
220 - I should come in 3rd in group 1 which is displayed first.
</div>
<div class="container">
<div class="terminal" data-price="740">
<h3>740</h3>
</div>
740 - I should come in 2nd in group 2 which is displayed last.
<div class="anotherGroup">Group 2</div>
</div>
<div class="container">
<div class="terminal" data-price="140">
<h3>140</h3>
</div>
140 - I should come in 1st in group 2 which is displayed last.
<div class="anotherGroup">Group 2</div>
</div>
<div class="container">
<div class="terminal" data-price="130">
<h3>130</h3>
</div>
130 - I should come in 1st in group 1 which is displayed first.
</div>
Script:
$('.terminal').sort(function (a, b) {
return $(a).data('price') - $(b).data('price');
}).map(function () {
return $(this).closest('.container');
}).each(function (_, container) {
$(container).parent().append(container);
});
The final result should be:
130
195
220
140
740
Where 140 and 740 belong in a group of their own because they contain an element with the classname "anotherGroup". Hope this makes sense. Any help is very much appreciated.