並べ替えたいオブジェクトの配列は次のようになります。
var students = [
{'name': 'AAA',
'year': 'freshman',
'score': 90,},
{'name': 'ABA',
'year': 'sophomore',
'score': 100},
...
];
カスタムソート機能:
function customOrder(value) {
switch(value){
case 'freshman':
return 0;
case 'sophomore':
return 1;
case 'junior':
return 2;
case 'senior':
return 3;
}
}
そして、Angular orderBy フィルターを使用しています:
students = $filter('orderBy')(students, function(item) {
return customOrder(item.year);
};
上記のコードを使用すると、学生の配列を年ごとに並べ替えることができます。しかし、配列を次のように並べ替えたい: year: DESC、次にname: ASC、次にscore: DESC。
このための配列を作成しました:
$scope.sortArray = ['name', '-score'];
そして、並べ替えロジックを次のように変更しました。
students = $filter('orderBy')(students, [function(item) {
return customOrder(item.year);
}, $scope.sortArray]); // I don't want to hard code here (i.e. 'name', '-score')
しかし、それはエラーを出します。
生徒の配列をソートする方法はありますか?