简体   繁体   English

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

[英]Sorting Array objects based on Time property

I have two arrays and I need to convert them into array object. 我有两个数组,我需要将它们转换为数组对象。 Data array at the end is the array object after I push two arrays into one array object. 将两个数组推入一个数组对象后,最后的数据数组就是数组对象。 Now I need to sort them based on time. 现在,我需要根据时间对它们进行排序。 I am new to js so that I would like to ask for a solution or pseudo code for this problem. 我是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' }
];

Use a standard javascript Array.prototype.sort() method with localeCompare 将标准的javascript Array.prototype.sort()方法与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 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

Make an iteration over machineName using map() array and create the object. 使用map()数组在machineName进行迭代并创建对象。 After formatting your expected data, sort the array based on time. 格式化期望的数据后,请根据时间对数组进行排序。

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

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

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