我有一系列JavaScript,可容纳学生及其课程安排;
tablo[0] = ['Molly','Class3A','2018.12.18','13:50'];
tablo[1] = ['Ashley','Class4C','2018.10.14','14:50'];
tablo[2] = ['John','Class3A','2018.01.01','13:50'];
tablo[3] = ['Molly','Class3A','2018.11.20','13:50'];
tablo[4] = ['John','Class3A','2018.12.18','15:50'];
tablo[5] = ['Molly','Class3A','2018.09.11','13:50'];
tablo[6] = ['Ashley','Class4C','2018.10.14','15:50'];
tablo[7] = ['Ashley','Class4C','2018.11.12','13:50'];
tablo[8] = ['John','Class3A','2018.01.01','18:50'];
tablo[9] = ['John','Class3A','2018.01.01','10:50'];
tablo[10] = ['Molly','Class3A','2018.12.31','13:50'];
tablo[11] = ['Ashley','Class4C','2018.10.14','08:50'];
tablo[12] = ['Molly','Class3A','2018.05.07','13:50'];
tablo[13] = ['John','Class3A','2018.01.01','07:50'];
tablo[14] = ['Molly','Class3A','2018.09.11','12:50'];
tablo[15] = ['Molly','Class3A','2018.09.11','15:50'];
tablo[16] = ['Ashley','Class4C','2018.11.12','10:50'];
我想先按班级名称,然后按学生姓名,然后按日期和最后按小时对这个数组进行排序。排序后的数组必须是这样;
tablo[0] = ['John','Class3A','2018.01.01','07:50'];
tablo[1] = ['John','Class3A','2018.01.01','10:50'];
tablo[2] = ['John','Class3A','2018.01.01','13:50'];
tablo[3] = ['John','Class3A','2018.01.01','18:50'];
tablo[4] = ['John','Class3A','2018.12.18','15:50'];
tablo[5] = ['Molly','Class3A','2018.05.07','13:50'];
tablo[6] = ['Molly','Class3A','2018.09.11','12:50'];
tablo[7] = ['Molly','Class3A','2018.09.11','13:50'];
tablo[8] = ['Molly','Class3A','2018.09.11','15:50'];
tablo[9] = ['Molly','Class3A','2018.11.20','13:50'];
tablo[10] = ['Molly','Class3A','2018.12.18','13:50'];
tablo[11] = ['Molly','Class3A','2018.12.31','13:50'];
tablo[12] = ['Ashley','Class4C','2018.10.14','08:50'];
tablo[13] = ['Ashley','Class4C','2018.10.14','14:50'];
tablo[14] = ['Ashley','Class4C','2018.10.14','15:50'];
tablo[15] = ['Ashley','Class4C','2018.11.12','10:50'];
tablo[16] = ['Ashley','Class4C','2018.11.12','13:50'];
有很多方法可以按2个条件进行排序,但是像上面的示例一样,我无法克服按4个条件进行排序。
答案 0 :(得分:2)
尝试一下
tablo.sort( (a,b)=> a[1].localeCompare(b[1]) || a[0].localeCompare(b[0]) || a[2].localeCompare(b[2]) || a[3].localeCompare(b[3]) );
tablo = [];
tablo[0] = ['Molly','Class3A','2018.12.18','13:50'];
tablo[1] = ['Ashley','Class4C','2018.10.14','14:50'];
tablo[2] = ['John','Class3A','2018.01.01','13:50'];
tablo[3] = ['Molly','Class3A','2018.11.20','13:50'];
tablo[4] = ['John','Class3A','2018.12.18','15:50'];
tablo[5] = ['Molly','Class3A','2018.09.11','13:50'];
tablo[6] = ['Ashley','Class4C','2018.10.14','15:50'];
tablo[7] = ['Ashley','Class4C','2018.11.12','13:50'];
tablo[8] = ['John','Class3A','2018.01.01','18:50'];
tablo[9] = ['John','Class3A','2018.01.01','10:50'];
tablo[10] = ['Molly','Class3A','2018.12.31','13:50'];
tablo[11] = ['Ashley','Class4C','2018.10.14','08:50'];
tablo[12] = ['Molly','Class3A','2018.05.07','13:50'];
tablo[13] = ['John','Class3A','2018.01.01','07:50'];
tablo[14] = ['Molly','Class3A','2018.09.11','12:50'];
tablo[15] = ['Molly','Class3A','2018.09.11','15:50'];
tablo[16] = ['Ashley','Class4C','2018.11.12','10:50'];
tablo.sort( (a,b)=> a[1].localeCompare(b[1]) || a[0].localeCompare(b[0]) || a[2].localeCompare(b[2]) || a[3].localeCompare(b[3]) );
console.log(tablo.map(x=>x.join(' ')));
答案 1 :(得分:1)
您可以使用Page_Validate()
对数据进行排序。
string#localeCompare
答案 2 :(得分:1)
您可以尝试一下。
tablo.sort(function (a, b) {
var res = a[1].localeCompare(b[1]); // Compare by classname
if(res == 0) { //Classnames are same, so comapre by student name
res = a[0].localeCompare(b[0]); //Compare by student name
if(res == 0){ //Student names are same, so comapre by date
res = a[2].localeCompare(b[2]); //Compare by date TODO: Use some date parsing library like "momentjs" if required
if(res == 0){ //Date is same, so comapre by hour
res = a[3].localeCompare(b[3])
}
}
}
return res
});