在JS数组上运行多个排序

时间:2018-06-15 08:37:28

标签: javascript jquery

我有这个JS数组......

   var arr = [{
	"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-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-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-033",
	"Interest_area": "",
	"Start_time": "9:00 AM",
	"End_time": "3:00 PM",
	"Session_type": "General information session",
	"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-060",
	"Interest_area": "Sport",
	"Start_time": "9:30 AM",
	"End_time": "3:00 PM",
	"Session_type": "Tour",
	"all_day_evt": true
}, {
	"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-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"
}, {
	"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-096",
	"Interest_area": "Media, Communication and creative arts",
	"Start_time": "10:00 AM",
	"End_time": "12:00 PM",
	"Session_type": "Experience",
	"all_day_evt": false
}, {
	"Event_code": "BW-163",
	"Interest_area": "Business",
	"Start_time": "10:00 AM",
	"End_time": "2:00 PM",
	"Session_type": "Experience",
	"all_day_evt": false,
	"clash": "This clashes with another session"
}, {
	"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-018",
	"Interest_area": "Sciences",
	"Start_time": "12:30 PM",
	"End_time": "2:30 PM",
	"Session_type": "Tour",
	"all_day_evt": false,
	"clash": "This clashes with another session"
}, {
	"Event_code": "BW-013",
	"Interest_area": "Sciences, Engineering, Information technology, Architecture and built environment, Environment",
	"Start_time": "12:30 PM",
	"End_time": "1:00 PM",
	"Session_type": "Course information session",
	"all_day_evt": false
}, {
	"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"
}]

    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);

我希望在这个阵列上做3次操作......

  1. 通过“all_day_evt”键对此数组进行分组。
  2. 在'true'子组中对此数组进行排序,并在'Start_time'中对'false'子组进行排序。
  3. 排序,如果'start_time'上有“碰撞”......“碰撞事件”出现在第二位。
  4. 我不确定如何实施第3个条件,非常感谢任何帮助。感谢

    下面显示的示例输出 - 突出显示冲突事件应该如何排列。它应该显示出来,以便“碰撞”键的事件排在第2位,而不是第1位。

    例如,事件“BW-013”& “BW-018”在下午12:30的开始时间发生冲突。因此,'BW-013'首先出现,然后'BW-018'出现。事件'BW-039'和'BW-162'也是如此,如下面的示例输出所示。

    [{
            "Event_code": "BW-013",
            "Interest_area": "Sciences, Engineering, Information technology, Architecture and built environment, Environment",
            "Start_time": "12:30 PM",
            "End_time": "1:00 PM",
            "Session_type": "Course information session",
            "all_day_evt": false
        },
        {
            "Event_code": "BW-018",
            "Interest_area": "Sciences",
            "Start_time": "12:30 PM",
            "End_time": "2: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"
        }
    ]
    

1 个答案:

答案 0 :(得分:1)

试试以下内容。您需要添加clash

的附加条件

 var arr = [{"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-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-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-033","Interest_area":"","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"General information session","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-060","Interest_area":"Sport","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"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-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"},{"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-096","Interest_area":"Media, Communication and creative arts","Start_time":"10:00 AM","End_time":"12:00 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-163","Interest_area":"Business","Start_time":"10:00 AM","End_time":"2:00 PM","Session_type":"Experience","all_day_evt":false,"clash":"This clashes with another session"},{"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-018","Interest_area":"Sciences","Start_time":"12:30 PM","End_time":"2:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-013","Interest_area":"Sciences, Engineering, Information technology, Architecture and built environment, Environment","Start_time":"12:30 PM","End_time":"1:00 PM","Session_type":"Course information session","all_day_evt":false},{"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"}];
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))) || Boolean(x.clash) - Boolean(y.clash));
console.log(arr);