3
2

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.

Javascriptで配列・オブジェクトの配列のソート

Last updated at Posted at 2018-10-28

よく使うのでメモ。JavascriptのバージョンはES2015。
次のような配列やオブジェクトの配列(連想配列の配列)があった場合、

//普通の配列
var array = [5,3,7];

//オブジェクトの配列
var objArray = [
    {'name':'apple','price':300},
    {'name':'orange','price':200},
    {'name':'banana','price':100}
];

#1 配列のソート
###関数

/**
* @function Array.prototype.sortArray 配列をソートする
*/
if(!Array.prototype.sortArray){
    Object.defineProperty(Array.prototype, 'sortArray', {
        configurable: false,
        enumerable: false,
        writable: false,
        /**
        * @string order ソートの順序 ('asc' or 'desc')
        */
        value: function(order) {
            switch(order){
                case 'asc':  //昇順
                    this.sort((a,b) => a-b);
                    break;
                case 'desc':  //降順
                    this.sort((a,b) => b-a);
                    break;
                default:  //昇順・降順の指定がない場合は降順でソート
                    this.sort((a,b) => b-a);
                    break;
            }
            return this;
        }
    });
}

###処理→出力

var array = [5,3,7];

console.log(array.sortArray('desc')); //降順でソート

###結果

[ 7, 5, 3 ]

#2 オブジェクトの配列のソート
###関数
キーから値を取得し、バリデーションを追加しただけで、基本的に配列のソートと同じ。

/**
* @function Array.prototype.sortObjArray オブジェクトの配列をソートする
*/
if(!Array.prototype.sortObjArray){
    Object.defineProperty(Array.prototype, 'sortObjArray', {
        configurable: false,
        enumerable: false,
        writable: false,
        /**
        * @string key ソートに用いるオブジェクトのキー
        * @string order ソートの順序 ('asc' or 'desc')
        */
        value: function(key,order) {
            //オブジェクトの配列であるか確認
            if(Object.prototype.toString.call(this[0])!=='[object Object]'){
                console.log('TypeError: Other than an array of object was passed.');
                return false;
            }
            switch(order){
                case 'asc':  //昇順
                    this.sort((a,b) => a[key]-b[key]);
                    break;
                case 'desc':  //降順
                    this.sort((a,b) => b[key]-a[key]);
                    break;
                default:  //昇順・降順の指定がない場合は降順でソート
                    this.sort((a,b) => b[key]-a[key]);
                    break;
            }
            return this;
        }
    });
}

###処理→出力

var objArray = [
    {'name':'apple','price':300},
    {'name':'orange','price':200},
    {'name':'banana','price':100}
];

console.log(objArray.sortObjArray('price','asc')); //昇順でソート

###結果

[ { name: 'banana', price: 100 },
  { name: 'orange', price: 200 },
  { name: 'apple', price: 300 } ]
3
2
0

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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?