首先按字符串排序数组,然后按日期排序

时间:2017-12-18 11:26:09

标签: javascript arrays sorting

好吧,我有一个带有随机值的数组对象, 实施例

var value = [
    {"date":"06/11/2017","status":"B"},
    {"date":"06/11/2017","status":"B"},
    {"date":"15/05/2017","status":"R"},
    {"date":"15/05/2017","status":"R"},
    {"date":"14/05/2018","status":"R"},
    {"date":"05/05/2017","status":"R"},
    {"date":null,"status":"W"},
    {"date":null,"status":"W"},
    {"date":null,"status":"W"},
    {"date":"05/11/2017","status":"B"},
    {"date":"27/07/2017","status":"R"},
    {"date":"14/05/2018","status":"R"},
    {"date":"27/07/201","status":"R"},
    {"date":"14/05/2018","status":"R"},
    {"date":"26/02/2018","status":"R"},
    {"date":null,"status":"W"}
];

我想首先按键status&然后按键date as,

输出:

var result =  [
    {"date":"05/11/2017","status":"B"}, 
    {"date":"06/11/2017","status":"B"},
    {"date":"06/11/2017","status":"B"},
    {"date":"05/05/2017","status":"R"},
    {"date":"15/05/2017","status":"R"},
    {"date":"15/05/2017","status":"R"},
    {"date":"27/07/2017","status":"R"},
    {"date":"14/05/2018","status":"R"},
    {"date":"14/05/2018","status":"R"},
    {"date":"14/05/2018","status":"R"},
    {"date":"26/02/2018","status":"R"},
    {"date":"27/07/2018","status":"R"},     
    {"date":null,"status":"W"},
    {"date":null,"status":"W"},
    {"date":null,"status":"W"},
    {"date":null,"status":"W"}
];

/*I tried few generic code to sort, */   
var result = value.sort(function (a, b) {
    var aValue = (a.date) ?  a.date: 0;
    var bValue = (b.date) ?  b.date: 0;
    return a.status - b.status ||  aValue - bValue;
});

我提到了几个例子SO Example,但没有得到预期的输出。请建议我最好的方法来获得这个。

5 个答案:

答案 0 :(得分:0)

这会有用吗?

value = value.sort(function (a, b) {
  if (a === b || (a.status === b.status && a.date === b.date)) return 0;

  if (a.status > b.status) return 1;
  if (a.status < b.status) return -1;

  if (a.date > b.date) return 1;
  if (a.date < b.date) return -1;
})

答案 1 :(得分:0)

GOOGLE_APPLICATION_CREDENTIALS

您正在处理的价值不是数字!你不能从另一个中减去一个。

另外:不要使用嵌套的三元运算符。这使得理解代码变得非常困难。

使用return a.status - b.status || aValue - bValue; 语句。它们更具可读性。

认为这就是你要找的东西,但你对这个问题的描述并不清楚。但是,这应该向您展示原则,以便您可以根据需要进行调整。

if

const a_is_first = -1; const b_is_first = 1; function compare(a, b) { // Sort and group by status first. Sort by date within the statuses. if (a.status < b.status) { return a_is_first; } else if (a.status > b.status) { return b_is_first; } else { // They are the same var a__date = convert_to_a_date_object(a.date); var b__date = convert_to_a_date_object(b.date); if (a__date < b__date) { return a_is_first; } else if (a__date > b__date) { return b_is_first; } else { return 0; } } } value.sort(compare); 的实施留给读者练习。请记住,它必须处理convert_to_a_date_object

答案 2 :(得分:0)

请查看是否有帮助

value.sort(function(a, b) {
  var nameA = a.status.toUpperCase(),
    nameB = b.status.toUpperCase();
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
}).sort(function(a, b) {
  return new Date(b.date) - new Date(a.date);
});

答案 3 :(得分:0)

您的日期格式为“dd / mm / yyyy”。您可以使用Date.parse("11/05/2017")(格式为“mm / dd / yyyy”,请参阅parse获取更多格式)以返回毫秒数并进行比较。对于status,您可以使用status.charCodeAt(0)获取ASCII值并进行比较。 假设您有一个好的日期格式,这是解决方案:

value.sort(function(a, b) {
  var diff = Date.parse(a.date) - Date.parse(b.date);
  if (diff === 0) {
    return a.status.charCodeAt(0) - b.status.charCodeAt(0);
  } 
  return diff;
});

希望它有所帮助。

答案 4 :(得分:0)

您可以对status进行排序,如果相等,则使用ISO 8601表示法中的日期字符串进行排序。 ISO 6801允许使用按字符串排序而不使用日期对象。

var array = [{ date: "06/11/2017", status: "B" }, { date: "06/11/2017", status: "B" }, { date: "15/05/2017", status: "R" }, { date: "15/05/2017", status: "R" }, { date: "14/05/2018", status: "R" }, { date: "05/05/2017", status: "R" }, { date: null, status: "W" }, { date: null, status: "W" }, { date: null, status: "W" }, { date: "05/11/2017", status: "B" }, { date: "27/07/2017", status: "R" }, { date: "14/05/2018", status: "R" }, { date: "27/07/2017", status: "R" }, { date: "14/05/2018", status: "R" }, { date: "26/02/2018", status: "R" }, { date: null, status: "W" }];

array.sort(function (a, b) {
    function getDate(d) {
        return d ? d.replace(/(..)\/(..)\/(....)/, '$3-$2-$1') : '';
    }

    return a.status.localeCompare(b.status) || getDate(a.date).localeCompare(getDate(b.date));
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }