简体   繁体   English

对Immutable.js列表进行排序和分组

[英]Sort and Group an Immutable.js List

Given the following data structure: 鉴于以下数据结构:

var MyData = [
  {"id": 1, "status": "live", dateCreated: "12:00:00 01/02/2016"}, 
  {"id": 2, "status": "draft", dateCreated: "13:00:00 03/12/2015"}, 
  {"id": 3, "status": "ready", dateCreated: "16:00:00 04/09/2016"}, 
  {"id": 4, "status": "ready", dateCreated: "10:00:00 01/10/2016"}, 
  {"id": 5, "status": "live", dateCreated: "09:00:00 05/07/2015"}, 
  {"id": 6, "status": "draft", dateCreated: "08:00:00 11/03/2016"}, 
  {"id": 7, "status": "ready", dateCreated: "20:00:00 12/02/2016"}
]

I'm trying to sort and group it into these conditions: 我正在尝试对它进行排序并将其分组到以下条件中:

  1. Grouped by status 按地位分组
  2. Ordered by status such that the order is "live", "draft", "ready" 按状态排序,订单是“现场”,“草稿”,“准备好”
  3. Items within each status should be ordered by dateCreated, most recent first. 每个状态中的项目应按dateCreated排序,最近一次。

What I have so far: 到目前为止我所拥有的:

// this object will help us define our custom order as it's not alphabetical
const itemOrder = {
  'live': 1, 
  'ready': 2,
  'draft': 3
};

const sortByStatus = (statusA, statusB) => {
  if ( itemOrder[statusA] > itemOrder[statusB] ) return 1;
  if ( itemOrder[statusA] < itemOrder[statusB] ) return -1;
  return 0;
};

return List(MyData)
  .groupBy(item => item.status)
  .sort( sortByStatus )

Ignore for a moment the fact that I've not got to the point where I can sort by date yet :) 暂时忽略了一个事实,即我没有达到我可以按日期排序的程度:)

The problem with the above seems to be that sortByStatus is being passed the IndexedIterable that is the overall group but not it's key so I can't sort it by that key. 上面的问题似乎是sortByStatus传递的是IndexedIterable,它是整个组但不是它的键,所以我不能用那个键对它进行排序。 I think I probably need to use sortBy but the Immutable.js docs are incomprehensible and have no examples on which to work out how to achieve this. 我可能需要使用sortBy但是Immutable.js文档是不可理解的,并且没有关于如何实现这一点的示例。

So, the question: how can I take the result of the groupBy action and sort it into a custom order and additionally how can I ensure that all the items in each group are sorted by date? 那么,问题是:如何获取groupBy操作的结果并将其排序为自定义顺序,另外如何确保每个组中的所有项目按日期排序?

One easy fix is to just reach in to the first item of the array and get the status from there: 一个简单的解决方法是直接进入数组的第一项并从那里获取状态:

 var MyData = [ {"id": 1, "status": "live", dateCreated: "12:00:00 01/02/2016"}, {"id": 2, "status": "draft", dateCreated: "13:00:00 03/12/2015"}, {"id": 3, "status": "ready", dateCreated: "16:00:00 04/09/2016"}, {"id": 4, "status": "ready", dateCreated: "10:00:00 01/10/2016"}, {"id": 5, "status": "live", dateCreated: "09:00:00 05/07/2015"}, {"id": 6, "status": "draft", dateCreated: "08:00:00 11/03/2016"}, {"id": 7, "status": "ready", dateCreated: "20:00:00 12/02/2016"} ] const itemOrder = { 'live': 1, 'ready': 2, 'draft': 3 }; const sortByStatus = (statusA, statusB) => { var a = itemOrder[statusA.get(0).status]; var b = itemOrder[statusB.get(0).status]; console.log(statusA.get(0).status, a, statusB.get(0).status, b) if ( a > b ) return 1; if (a < b ) return -1; return 0; }; var result = Immutable.List(MyData) .groupBy(item => item.status) .sort( sortByStatus ); console.log(result.toJS()) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script> 

I think you were almost there. 我想你差不多了。 The important thing to notice is that .groupBy is returning a collection of lists, so when you call sort on that, you need a comparator that compares two different lists rather than two items. 需要注意的重要一点是.groupBy返回一个列表集合,所以当你调用sort时,你需要一个比较器来比较两个不同的列表而不是两个项目。 You can do this easily by just comparing the status of the first item in each list. 只需比较每个列表中第一项的状态,即可轻松完成此操作。 After that, you want to sort each list individually by date, so you use .map to apply a change to each list in your list, then .sortBy on that list to sort by a specific key. 之后,您希望按日期对每个列表进行单独排序,因此您使用.map将更改应用于列表中的每个列表,然后.sortBy在该列表上按特定键排序。 Assuming you are using the built in Date type, just sorting by that field should do what you want . 假设您使用的是内置日期类型, 只需按该字段排序就可以执行您想要的操作

var MyData = [
  {"id": 1, "status": "live", dateCreated: "12:00:00 01/02/2016"}, 
  {"id": 2, "status": "draft", dateCreated: "13:00:00 03/12/2015"}, 
  {"id": 3, "status": "ready", dateCreated: "16:00:00 04/09/2016"}, 
  {"id": 4, "status": "ready", dateCreated: "10:00:00 01/10/2016"}, 
  {"id": 5, "status": "live", dateCreated: "09:00:00 05/07/2015"}, 
  {"id": 6, "status": "draft", dateCreated: "08:00:00 11/03/2016"}, 
  {"id": 7, "status": "ready", dateCreated: "20:00:00 12/02/2016"}
]


Immutable.fromJs(MyData)
  // [{id: 1, ...}, ...]
  .groupBy(item => item.status) // group items into lists by status
  // [ 'live': [ { id: 1, ... }, { id:5, ... } ],
  //   'draft': [ { id: 2, ... }, { id: 6, ...} ],
  //   'ready': [ { id: 3, ... }, { id: 4, ... }, { id: 7, ... } ] ]
  .sort((listA, listB) => // order these groups by status
    itemOrder[listA.first().status] - itemOrder[listB.first().status])
  // [ 'live': [ { id: 1, ...}, ... ],
  //   'ready': [ { id: 3, ...}, ... ], 
  //   'draft': [ { id: 2, ...}, ... ] ]
  .map(list => list.sortBy(item => item.dateCreated)); // sort the elements in each group by date
  // [ 'live': [ { id: 5, ... }, { id: 1, ... } ],
  //   'ready': [ { id: 4, ... }, { id: 3, ... }, { id: 7, ... } ], 
  //   'draft': [ { id: 2, , ...}, { id: 6, ... } ] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM