13
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JS] Javascript (+jQuery)で要素をソートする

Last updated at Posted at 2016-04-18

やんごとなき事情によりJavascriptでのソートが必要になったらこうする。
Javascriptネイティブ関数のsortを使う。

// 降順にソートするサンプル
$(function(){
    var $items_wrapper = $('ul');
    var $items = $('ul li');
    $items.sort(function(a, b){
        return parseInt($(a).text()) < parseInt($(b).text()); 
    });
    $items_wrapper.html("");
    $items.each(function(){
        $items_wrapper.append($(this));
    })
});

サンプル
http://jsrun.it/jsmz/CFix

13
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?