子组内的JS数组排序 [英] JS array sort within subgroups

查看:94
本文介绍了子组内的JS数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个JS数组....

I have this JS Array ....

[{
  "Event_code": "BW-114",
  "Interest_area": "Nursing and midwifery",
  "Start_time": "9:00 AM",
  "End_time": "3:00 PM",
  "Session_type": "Tour",
  "all_day_evt": true
}, {
  "Event_code": "BW-087",
  "Interest_area": "Information technology",
  "Start_time": "9:00 AM",
  "End_time": "3:00 PM",
  "Session_type": "Experience",
  "all_day_evt": true
}, {
  "Event_code": "BW-060",
  "Interest_area": "Sport",
  "Start_time": "9:30 AM",
  "End_time": "3:00 PM",
  "Session_type": "Tour",
  "all_day_evt": true
}, {
  "Event_code": "BW-115",
  "Interest_area": "Food, Nutrition and dietetics",
  "Start_time": "9:30 AM",
  "End_time": "3:00 PM",
  "Session_type": "Tour",
  "all_day_evt": true
}, {
  "Event_code": "BW-161",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "9:00 AM",
  "End_time": "3:00 PM",
  "Session_type": "Experience",
  "all_day_evt": true
}, {
  "Event_code": "BW-032",
  "Interest_area": "",
  "Start_time": "9:30 AM",
  "End_time": "10:00 AM",
  "Session_type": "General information session",
  "all_day_evt": false
}, {
  "Event_code": "BW-129",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "2:00 PM",
  "End_time": "2:30 PM",
  "Session_type": "Tour",
  "all_day_evt": false
}, {
  "Event_code": "BW-081",
  "Interest_area": "Information technology",
  "Start_time": "9:00 AM",
  "End_time": "9:30 AM",
  "Session_type": "Course information session",
  "all_day_evt": false
}, {
  "Event_code": "BW-048",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "12:00 PM",
  "End_time": "12:30 PM",
  "Session_type": "Experience",
  "all_day_evt": false
}, {
  "Event_code": "BW-128",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "12:00 PM",
  "End_time": "12:30 PM",
  "Session_type": "Tour",
  "all_day_evt": false,
  "clash": "This clashes with another session"
}, {
  "Event_code": "BW-039",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "1:00 PM",
  "End_time": "1:30 PM",
  "Session_type": "Experience",
  "all_day_evt": false
}, {
  "Event_code": "BW-162",
  "Interest_area": "Education and teaching",
  "Start_time": "1:00 PM",
  "End_time": "1:30 PM",
  "Session_type": "Tour",
  "all_day_evt": false,
  "clash": "This clashes with another session"
}, {
  "Event_code": "BW-034",
  "Interest_area": "",
  "Start_time": "1:30 PM",
  "End_time": "2:00 PM",
  "Session_type": "General information session",
  "all_day_evt": false
}, {
  "Event_code": "BW-170",
  "Interest_area": "",
  "Start_time": "9:30 AM",
  "End_time": "10:30 AM",
  "Session_type": "General information session",
  "all_day_evt": false,
  "clash": "This clashes with another session"
}]

这是我的代码...

// Func 1 -- Sort by 'all_day_evt' key i.e. True/False

arr.sort(function (x, y) {
  return y.all_day_evt - x.all_day_evt;
});
console.log(arr);

//  Func 2 -- Sort by time

arr.sort(function (a, b) {
  return ((new Date('1970/01/01 ' + a.Start_time)) - (new Date('1970/01/01 ' + b.Start_time)));
});
console.log(arr);

我正在对此数组执行2次操作...

I am looking to do 2 operations on this array ...

  1. 通过"all_day_evt"键将该数组分组.我可以使用Func 1轻松做到这一点.
  2. 在"true"子组和"false"子组中按"Start_time"对数组进行排序.我可以使用Func 2轻松地做到这一点,但只能在所有整个组/阵列上进行.

如何在子组(即"true"组和"false"组)中执行此操作?合并这两个功能时遇到一些困难.

How do I do this within the subgroups i.e. group of 'true' and group of 'false' ? I am having some difficulties combining these 2 functions.

任何建议都非常感谢.谢谢.

Any advise is very appreciated. Thanks.

推荐答案

您需要按两个字段进行排序.尝试关注

You need to sort by both the fields together. Try following

let arr = [{"Event_code":"BW-114","Interest_area":"Nursing and midwifery","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-087","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-060","Interest_area":"Sport","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-115","Interest_area":"Food, Nutrition and dietetics","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-161","Interest_area":"Media, Communication and creative arts","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-032","Interest_area":"","Start_time":"9:30 AM","End_time":"10:00 AM","Session_type":"General information session","all_day_evt":false},{"Event_code":"BW-129","Interest_area":"Media, Communication and creative arts","Start_time":"2:00 PM","End_time":"2:30 PM","Session_type":"Tour","all_day_evt":false},{"Event_code":"BW-081","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"9:30 AM","Session_type":"Course information session","all_day_evt":false},{"Event_code":"BW-048","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-128","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-039","Interest_area":"Media, Communication and creative arts","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-162","Interest_area":"Education and teaching","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-034","Interest_area":"","Start_time":"1:30 PM","End_time":"2:00 PM","Session_type":"General information session","all_day_evt":false},{"Event_code":"BW-170","Interest_area":"","Start_time":"9:30 AM","End_time":"10:30 AM","Session_type":"General information session","all_day_evt":false,"clash":"This clashes with another session"}];

arr.sort((x,y) => y.all_day_evt- x.all_day_evt || ((new Date('1970/01/01 ' + x.Start_time)) - (new Date('1970/01/01 ' + y.Start_time))));
console.log(arr);

这篇关于子组内的JS数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆