Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

javascript - Running multiple sorts on JS array

I have this JS Array ...

   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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try following. You need to add additional condition of 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...