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

时间:2017-11-25 08:41:58

标签: javascript 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']
]

这是我想要的输出:

[
  ['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']
]

注意输出如何按天,小时和名字的第一个字母

排序

以下是我的代码。我能够很容易地按天和小时排序,但我找不到比较和放置名字的第一个字母的方法:

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

我尝试按日期排序一次,然后执行另一个排序功能,比较每个名字的第一个字母,但我遇到问题,因为它没有正确排序数据:

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

我可以在哪些方面修改此代码以实现我想要的输出?任何帮助将不胜感激。提前谢谢!

3 个答案:

答案 0 :(得分:3)

您可以将第二个元素的增量视为日期差异,如果增量为日期的零,则将第一个字符用于排序

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我们应该检查日期差异,只有当它为零时 - 我们按第一个字符排序,请参阅代码注释:

&#13;
&#13;
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)
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用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);