AngularJSのng-repeatのorderByはArrayじゃないとソートされない
Created: 4/3/2014, 12:00:00 AM
AngularJSのng-repeatに以下のようにorderBy書くとソートしてくれます。
便利!
※’-age’を’age’にすれば昇順になったりします。
だけどloopするオブジェクトがArrayじゃないとソートされない。
Javascriptの言葉の定義をちゃんと知らないので誤解のないようにソースコードで示すと以下の通り。
以下はソートされる(Array?)
$scope.friends =
[{name:'John', phone:'555-1212', age:10},
{name:'Mary', phone:'555-9876', age:19},
{name:'Mike', phone:'555-4321', age:21},
{name:'Adam', phone:'555-5678', age:35},
{name:'Julie', phone:'555-8765', age:29}]
以下はソートされない(普通のObject?)
$scope.friends = {
'0':{name:'John', phone:'555-1212', age:10},
'1':{name:'Mary', phone:'555-9876', age:19},
'2':{name:'Mike', phone:'555-4321', age:21},
'3':{name:'Adam', phone:'555-5678', age:35},
'4':{name:'Julie', phone:'555-8765', age:29}
}
こんなデータあるのかと思うけど結構あります。
GoInstantでKeyとか扱うと多いです。