多维数组排序 JavaScript

问题描述 投票:0回答:3

我必须对此进行排序: 我正在从两个不同的

select
查询的数据库中获取数据,并且必须按
mpriority
mdate
对它们进行排序。

我知道如何对对象数组进行排序,但我不能使用

object
object.key

    var allocations = new Array(); 
    allocations[0] =
    [   qtA =  15,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp=  11,
        mpriority=  5,
        mdate=  new Date('2017-04-27 11:26:02.147'),
    ];

allocations[1] =
    [
        qtA= 13,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 5,
        mdate= new Date('2016-10-07 00:00:00.000'),
    ];

allocations[2] =
    [
        qtA= 16,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 4,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

allocations[3] =
    [
        qtA= 95,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 1,
        mdate= new Date('2016-10-06 00:00:00.000'),
    ];

allocations[4] =
    [
        qtA= 75,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 8,
        mdate= new Date('2017-02-20 12:41:34.903'),
    ];

allocations[5] =
    [
        qtA= 45,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 0,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

排序功能应首先查看

mpriority
- 如果两个值相同,则检查
mdate

我想应该是这样的

var sortOptions = {
    byPriorityAndDate: function (a, b) {
        return (b.mpriority - a.mpriority) || (a.mdate - b.mdate);
    },
}

以及如何检查我正在使用哪个版本的 JavaScript?

javascript arrays sorting multidimensional-array
3个回答
2
投票

你有数组的数组,而不是对象的数组。

我建议您将数组数组转换为对象数组,实例化每个元素,如下所示:

allocations[0] = {
    qtA:  15,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp:  11,
    mpriority:  5,
    mdate:  new Date('2017-04-27 11:26:02.147'),
};

这样您编写的用于比较的函数就应该可以工作了

另外,我建议您阅读使用 JavaScript 比较两个日期,了解您想要哪种比较

无论如何,这个排序功能将适合您的情况:

allocations = allocations.sort((a, b) => {
   if (a[4] === b[4]) {
        return a[5].getTime() - b[5].getTime();
   }

   return a[4] - b[4];
})

var allocations = new Array(); 
    allocations[0] =
    [   qtA =  15,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp=  11,
        mpriority=  5,
        mdate=  new Date('2017-04-27 11:26:02.147'),
    ];

allocations[1] =
    [
        qtA= 13,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 5,
        mdate= new Date('2016-10-07 00:00:00.000'),
    ];

allocations[2] =
    [
        qtA= 16,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 4,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

allocations[3] =
    [
        qtA= 95,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 1,
        mdate= new Date('2016-10-06 00:00:00.000'),
    ];

allocations[4] =
    [
        qtA= 75,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 8,
        mdate= new Date('2017-02-20 12:41:34.903'),
    ];

allocations[5] =
    [
        qtA= 45,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 0,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

allocations = allocations.sort((a, b) => {
   if (a[4] === b[4]) {
        return a[5].getTime() - b[5].getTime();
   }
   
   return a[4] - b[4];
})

console.log(allocations)


1
投票

您初始化数组的方式是错误的。尽管它运行时不会出现错误,但它所做的事情与您期望的不同。

这个:

allocations[0] =
[   qtA =  15,
    mdata=  '1234',
    mid =  '234234234',
    qtyToDoTemp=  11,
    mpriority=  5,
    mdate=  new Date('2017-04-27 11:26:02.147'),
];

...实际上会为全局变量赋值,如下所示:

qtA =  15,
mdata=  '1234',
mid =  '234234234',
qtyToDoTemp=  11,
mpriority=  5,
mdate=  new Date('2017-04-27 11:26:02.147')

然后数组将在索引 0 处获得一个具有以下值的嵌套数组:

allocations[0] = [
    15,
    '1234',
    '234234234',
    11,
    5,
    new Date('2017-04-27 11:26:02.147'),
];

因此,代码的结果是嵌套数组的数组,而不是对象的数组。

要创建对象数组,请使用以下语法:

var allocations = [{
    qtA: 15,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp:  11,
    mpriority:  5,
    mdate:  new Date('2017-04-27 11:26:02.147'),
}, {
    qtA: 13,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 5,
    mdate: new Date('2016-10-07 00:00:00.000'),
}, {
    // ...etc, etc
}];

现在你的函数就可以工作了:

var sortOptions = {
    byPriorityAndDate: function (a, b) {
        return (b.mpriority - a.mpriority) || (a.mdate - b.mdate);
    },
}

var allocations = [{
    qtA: 15,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp:  11,
    mpriority:  5,
    mdate:  new Date('2017-04-27 11:26:02.147'),
}, {
    qtA: 13,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 5,
    mdate: new Date('2016-10-07 00:00:00.000'),
}, {
    qtA: 16,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 4,
    mdate: new Date('2017-04-27 11:26:02.147'),
}, {
    qtA: 95,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 1,
    mdate: new Date('2016-10-06 00:00:00.000'),
}, {
    qtA: 75,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 8,
    mdate: new Date('2017-02-20 12:41:34.903'),
}, {
    qtA: 45,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp: 11,
    mpriority: 0,
    mdate: new Date('2017-04-27 11:26:02.147'),
}];

allocations.sort(sortOptions.byPriorityAndDate);

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


0
投票

好的,我在这里重写了您的代码,以便它执行我认为您希望它执行的操作:

var allocations = []; 
allocations.push({
  qtA: 15,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 5,
  mdate: new Date('2017-04-27 11:26:02.147')
});

allocations.push({
  qtA: 13,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 5,
  mdate: new Date('2016-10-07 00:00:00.000')
});

allocations.push({
  qtA: 16,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 4,
  mdate: new Date('2017-04-27 11:26:02.147')
});

allocations.push({
  qtA: 95,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 1,
  mdate: new Date('2016-10-06 00:00:00.000')
});

allocations.push({
  qtA: 75,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 8,
  mdate: new Date('2017-02-20 12:41:34.903')
});

allocations.push({
  qtA: 45,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 0,
  mdate: new Date('2017-04-27 11:26:02.147')
});

如果您不熟悉 Javascript 并且来自不同的语言,请知道这一点:JS 中的数组与其他语言中的数组不同,它们是具有特殊语法的对象,并尽力表现得像数组。

© www.soinside.com 2019 - 2024. All rights reserved.