简体   繁体   English

具有多个条件的JavaScript排序函数

[英]JavaScript Sort Function with more than one criteria

Here is a sample of what I am sorting: 以下是我正在排序的示例:

[
  ['Zed Jones', '24 Nov 2017 2:00 PM'],
  ['Jack Mo', '25 Nov 2017 5:00 PM'],
  ['John Phil', '25 Nov 2017 4:00 PM'],
  ['Bob Phil', '25 Nov 2017 4:00 PM']
]

Here is my desired output: 这是我想要的输出:

[
  ['Zed Jones', '24 Nov 2017 2:00 PM'],
  ['Bob Phil', '25 Nov 2017 4:00 PM'],
  ['John Phil', '25 Nov 2017 4:00 PM'],
  ['Jack Mo', '25 Nov 2017 5:00 PM']
]

Notice how the output is sorted by day, hour, and first letter of first name 注意输出是如何按名字的日,小时和首字母排序的

Here is my code below. 这是我的代码如下。 I have been able to sort by day and hour pretty easily but I am having trouble finding a way to compare and place first letter of first name: 我已经能够很容易地按天和小时排序,但我找不到比较和放置名字的第一个字母的方法:

function sortTable(data) {
  return data.sort((elem1, elem2) => {
    var dateA      = new Date(elem1[1])
      , dateB      = new Date(elem2[1])

    return dateA.getHours() - dateB.getHours() + dateB.setHours(0) - dateA.setHours(0);
  });
}

I have tried to sort once by date and then perform another sort function comparing the first letter of each first name, but I am having issues as it does not properly sort the data: 我试图按日期排序一次,然后执行另一个排序函数比较每个名字的第一个字母,但我遇到问题,因为它没有正确排序数据:

function sortTable(data) {
  // Sort by date and time
  data = data.sort((elem1, elem2) => {
    var dateA      = new Date(elem1[1])
      , dateB      = new Date(elem2[1])

    return dateA.getHours() - dateB.getHours() + dateB.setHours(0) - dateA.setHours(0);
  });

  // Then sort by name and return data
  return data.sort((elem1, elem2) => {
    var name1 = elem1[0]
      , name2 = elem2[0]
      , let1
      , let2;

    // Check that we have a name available
    if (name1 !== undefined) let1 = name1.charAt(0);
    if (name2 !== undefined) let2 = name2.charAt(0);

      return let1 < let2;
  });
 }

In what ways can I modify this code to achieve my desired output? 我可以在哪些方面修改此代码以实现我想要的输出? Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you in advance! 先感谢您!

You could take the delta of the second element as date difference and take the first character for sorting if the delta is zero of the date 您可以将第二个元素的增量作为日期差异,如果增量为日期的零,则将第一个字符用于排序

 var data = [['Zed Jones', '24 Nov 2017 2:00 PM'], ['Jack Mo', '25 Nov 2017 5:00 PM'], ['John Phil', '25 Nov 2017 4:00 PM'], ['Bob Phil', '25 Nov 2017 4:00 PM']]; data.sort(function (a, b) { return new Date(a[1]) - new Date(b[1]) || a[0][0] > b[0][0] || -(a[0][0] < b[0][0]); }); console.log(data); 

We should check the date-diff and only if it's zero - we'll sort by the first character, see code-comments: 我们应该检查date-diff,只有它是零 - 我们将按第一个字符排序,参见code-comments:

 var data = [ ['Zed Jones', '24 Nov 2017 2:00 PM'], ['Jack Mo', '25 Nov 2017 5:00 PM'], ['John Phil', '25 Nov 2017 4:00 PM'], ['Bob Phil', '25 Nov 2017 4:00 PM'] ]; function sortTable(data) { // Then sort by name and return data return data.sort((elem1, elem2) => { var dateA = new Date(elem1[1]) , dateB = new Date(elem2[1]); var name1 = elem1[0] , name2 = elem2[0] , let1 , let2; var dateDiff = dateA - dateB; // compare dates first // if we have a date-difference we'll use it if (dateDiff !== 0) { return dateDiff; } // only if no date-diff - compare names! if (name1 !== undefined) let1 = name1.charAt(0); if (name2 !== undefined) let2 = name2.charAt(0); return let1 > let2; // change the comparison from < to > }); } sortTable(data); console.log(data) 

You can use String.prototype.localeCompare 您可以使用String.prototype.localeCompare

 var input = [['Zed Jones', '24 Nov 2017 2:00 PM'], ['Jack Mo', '25 Nov 2017 5:00 PM'], ['John Phil', '25 Nov 2017 4:00 PM'], ['Bob Phil', '25 Nov 2017 4:00 PM'], ['Bob Phil', '01 Dec 2017 01:00 AM'], ['1Bob Phil', '01 Dec 2017 01:00 AM'], ['asdf df', '01 Dec 1999 01:30 PM']]; //Now with the help of `localeCompare` function sortTable(data) { return data.sort((elem1, elem2) => { var dateA = new Date(elem1[1]) , dateB = new Date(elem2[1]) , nameA = elem1[0] , nameB = elem2[0] , datecomp = dateA - dateB , namecomp = nameA.localeCompare(nameB); return datecomp > 0 ? datecomp : datecomp + namecomp; }); } //See results:- sortTable(input); console.log(input); 

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

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