1. 程式人生 > >lodash物件陣列排序

lodash物件陣列排序

_.sortBy([1, 2, 3], function(n) {
 return Math.sin(n);
});

以下程式碼示例:

var arr=
[
{"key":"key1","value":"value1","createTime":"124573216"},
{"key":"key2","value":"value2","createTime":"124593216"},
{"key":"key3","value":"value3","createTime":"124596216"},
{"key":"key4","value":"value4","createTime":"124596286"},
{"key":"key5","value":"value5","createTime":"124596289"},
]

 當要對createTime列進行排序時:

正序:

_.sortBy(arr, function(item) {
  return item.createTime;
});

倒序:

_.sortBy(arr, function(item) {
  return -item.createTime;
});

注意前面的負號