6

I have the following HTML structure:

<div id="container">
    <div>1</div>
    <div class="red">2</div>
    <div class="red">3</div>
    <div>4</div>
    <div>5</div>
    <div class="red">6</div>
    <div>7</div>
</div>

I wast to run some Jquery that will sort the divs inside the div container by ordering the divs first that have class="red", and then those that don't, so the final structure should be:

<div id="container">
    <div class="red">2</div>
    <div class="red">3</div>
    <div class="red">6</div>
    <div>1</div>
    <div>4</div>
    <div>5</div>
    <div>7</div>
</div>

Help? Thanks.

4
  • Bonus points if the re-ordering is done with with some sort of animation, to make it obvious to the user that the div order has changed. Thanks. Commented Jun 10, 2013 at 5:04
  • Can you please post the JavaScript/jQuery you've written so far? Commented Jun 10, 2013 at 5:05
  • Just curious. In the first place, why have you arranged them that way? Commented Jun 10, 2013 at 5:05
  • 6
    Bonus Points? same is for you, if you try soimething by yourself., before posting it Commented Jun 10, 2013 at 5:07

4 Answers 4

19

Try this:

 $(function(){
   var elem = $('#container').find('div').sort(sortMe);
   $('#container').append(elem);
 });

 function sortMe(a, b) {
        return a.className < b.className;
  }

Demo

With Some fadeIn/fadeout animation

var elem = $('#container').find('div').sort(sortByClass);

 function sortByClass(a, b) {
    return a.className < b.className;
 }

 var allElem = elem.get();
  (function append() {

    var $this = $(allElem.shift());

     $('#container').append(
                $this.fadeOut('slow'))
                         .find($this)
                             .fadeIn('slow', function () {
                                      if (allElem.length > 0) 
                                          window.setTimeout(append);
                            });
      })();

Demo

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

1 Comment

Hey this works great thanks! Is it possible to make it work with the second class name? Thanks
5

Sorting is not required here. As the elements are already in the order you need, you can just call prependTo() on them, like this:

$('.red').prependTo('#container');
.red { color: #C00; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="container">
    <div>1</div>
    <div class="red">2</div>
    <div class="red">3</div>
    <div>4</div>
    <div>5</div>
    <div class="red">6</div>
    <div>7</div>
</div>

1 Comment

Best answer. Works like a charm
3

Add 2 buttons to your html

<button class="sort">SORT</button>
<button class="unsort">UNSORT</button>

Attach click handlers...

$('.sort').click(function(){
 var elem = $('#container').find('div').sort(doSort);
 $('#container').append(elem);
}

$('.unsort').click(function(){
 var elem = $('#container').find('div').sort(doUnsort);
 $('#container').append(elem);
}

Sorting functions

function doSort(a, b) {
 return a.className < b.className;
}

function doUnsort(a, b) {
   var a = $(a).text().toUpperCase();
   var b = $(b).text().toUpperCase();
   return (a < b) ? -1 : (a > b) ? 1 : 0;
}

JS FIDDLE DEMO

Comments

2

Following PSL's answer, I had to add ? 1 : -1 to the sortMe function like so:

$(function(){
   var elem = $('#container').find('div').sort(sortMe);
   $('#container').append(elem);
 });

 function sortMe(a, b) {
        return a.className < b.className ? -1 : 1; // This is the added code
  }

Thanks to Alex and Rejith who answered my similar question How to _prevent_ divs from appearing in random order

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.