根据“时间”属性对数组对象进行排序

时间:2019-08-05 06:29:01

标签: javascript arrays sorting google-apps-script

我有两个数组,我需要将它们转换为数组对象。将两个数组推入一个数组对象后,最后的数据数组就是数组对象。 现在,我需要根据时间对它们进行排序。我是JS新手,所以我想为这个问题寻求解决方案或伪代码。

var arrayMachineName = ['V0101', 'V0103', 'V0102', 'V0201', 'V0202', 'V1101', 'V1202', 'V0503', 'V1102', 'V1601', 'V0602', 'V1201', 'V1702'];
var outOfResinTimeArray = ['19:20', '17:30', '13:20', '12:30', '21:20', '12:30', '03:30', '07:20', '04:30', '21:20', '22:30', '16:20', '14:30'];

function outOfResinTimeSorting(arrayMachineName, outOfResinTimeArray) {
    var cellOneToSixdataObj = [];
    var cellSevenToTwelvedataObj = [];
    var cellThirdTeenToSevenTeendataObj = [];
    for (var machineCounter = 0; machineCounter < arrayMachineName.length; machineCounter++) {
        var machineCode = parseInt(arrayMachineName[machineCounter].slice(1,3));
        if (machineCode >= 1 && machineCode <= 6) { 
            cellOneToSixdataObj.push({machine: arrayMachineName[machineCounter], time: outOfResinTimeArray[machineCounter]});
        }   
        else if (machineCode > 6 && machineCode <= 12) { 
            cellSevenToTwelvedataObj.push({machine: arrayMachineName[machineCounter], time: outOfResinTimeArray[machineCounter]});
        }
        else if (machineCode > 12 && machineCode <= 17) {
            cellThirdTeenToSevenTeendataObj.push({machine: arrayMachineName[machineCounter], time: outOfResinTimeArray[machineCounter]});
            }
        }
    console.log(cellOneToSixdataObj);
    console.log(cellSevenToTwelvedataObj);
    console.log(cellThirdTeenToSevenTeendataObj);
    var sortable = [];
    for (var machine in cellOneToSixdataObj){
        sortable.push([machine, cellOneToSixdataObj[machine]]);
    }
}

outOfResinTimeSorting(arrayMachineName, outOfResinTimeArray);


var Data =[
    { machine: 'V0101', time: '19:20' },
    { machine: 'V0103', time: '17:30' },
    { machine: 'V0102', time: '13:20' },
    { machine: 'V0201', time: '12:30' },
    { machine: 'V0202', time: '21:20' },
    { machine: 'V0503', time: '07:20' },
    { machine: 'V0602', time: '22:30' }
];

2 个答案:

答案 0 :(得分:1)

Array.prototype.sort()使用标准的JavaScript localeCompare方法

Data.sort(function (a, b) {
    return a.time.localeCompare(b.time);
});

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

答案 1 :(得分:0)

使用map()数组在machineName上进行迭代并创建对象。格式化期望的数据后,请根据时间对数组进行排序。

var arrayMachineName = ['V0101', 'V0103', 'V0102', 'V0201', 'V0202', 'V1101', 'V1202', 'V0503', 'V1102', 'V1601', 'V0602', 'V1201', 'V1702'];
var outOfResinTimeArray = ['19:20', '17:30', '13:20', '12:30', '21:20', '12:30', '03:30', '07:20', '04:30', '21:20', '22:30', '16:20', '14:30'];

// Format data array
var data = arrayMachineName.map(function(m, i) {
    return {machine: m, time: outOfResinTimeArray[i]};
});

// Sort data array base on time
data.sort(function(a, b) {
    return Number(a.time.replace(':', '')) < Number(b.time.replace(':', '')) ? -1 : 1
});

console.log(data);