按日期排序列表 Immutable JS
Sort List by date Immutable JS
我有一个 Immutable JS
List
对象。在每个对象中都有一个键 date
和 UNIX timestamp
[{someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734}]
如何按日期对列表进行排序?
您可以使用 Array#from
创建一个副本,然后对其进行排序。这不会改变原来的数组。
另外,Array#form
可以用在ImmutableJS列表上,会return对应的JS数组。
let data = [{someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734}];
console.log(Array.from(data).sort((a, b) => a.date - b.date));
console.log(data);
Only a select few methods can be used in withMutations including set, push and pop. These methods can be applied directly against a persistent data-structure where other methods like map, filter, sort, and splice will always return new immutable data-structures and never mutate a mutable collection.
var list = Immutable.List.of({someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734});
var sortedList = list.sort(function(lhs, rhs) {
return lhs.date > rhs.date;
});
console.log(list);
console.log(sortedList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>
我有一个 Immutable JS
List
对象。在每个对象中都有一个键 date
和 UNIX timestamp
[{someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734}]
如何按日期对列表进行排序?
您可以使用 Array#from
创建一个副本,然后对其进行排序。这不会改变原来的数组。
另外,Array#form
可以用在ImmutableJS列表上,会return对应的JS数组。
let data = [{someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734}];
console.log(Array.from(data).sort((a, b) => a.date - b.date));
console.log(data);
Only a select few methods can be used in withMutations including set, push and pop. These methods can be applied directly against a persistent data-structure where other methods like map, filter, sort, and splice will always return new immutable data-structures and never mutate a mutable collection.
var list = Immutable.List.of({someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734});
var sortedList = list.sort(function(lhs, rhs) {
return lhs.date > rhs.date;
});
console.log(list);
console.log(sortedList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>