Dave-88
Dave-88

Reputation: 227

sort div class by name jQuery

which is the shortest and best way to sort div class by name not by alphabetic name.

Here is my Testsite:

http://devauth.labscope.de/htmlapp/report-overview_test.html

I will to sort in the content div class so:

  1. div class="dark_red_gradient"
  2. div class="red_gradient"
  3. div class="orange_gradient"
  4. div class="yellow_gradient"
  5. div class="white_gradient"

How can i solve this? I know i can to give my class dark_red_1 but i will solve this without that.

I hope someone have idea.

Upvotes: 0

Views: 1299

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

Try

var array = ['dark_red_gradient', 'red_gradient', 'orange_gradient', 'yellow_gradient', 'white_gradient'];

var $ul = $('#mytest');
var sorted = $ul.children('li').get().sort(function (o1, o2) {
    return $.inArray(o1.className, array) - $.inArray(o2.className, array)
});
$.each(sorted, function () {
    $(this).appendTo($ul)
});

Assuming mytest is the id of the container li element.

Upvotes: 1

Related Questions