suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Sortierproblem bei zweidimensionalen JavaScript-Arrays

[['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']]

JavaScript So erstellen Sie ein Array wie dieses
[['7','6'],['8','7'],['9','5'],['10','8'] , ['11','4'],['18','7']]
in dieser Reihenfolge angeordnet

Großer Gott

仅有的幸福仅有的幸福2755 Tage vor788

Antworte allen(4)Ich werde antworten

  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:29:27

    array = [['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']]
    function sortNumber(a,b){return a[0]-b[0]}
    let newArray = array.sort(sortNumber)

    newArray 应该就是你要的结果了

    Antwort
    0
  • 代言

    代言2017-06-12 09:29:27

    var arr = [['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']]
    arr.sort(function(a, b) {
        return a[0]-b[0]
    })

    Antwort
    0
  • PHP中文网

    PHP中文网2017-06-12 09:29:27

    你这个二维数组的排列的结果,好像是用的数组中的每个元素(数组)中的第一个下标的元素去比较的,那就转化成一个一维数组排序不就不好了吗?一维数组排好序,再将原二维数组利用index对应上一维数组排序。
    逻辑实现思路:
    var twoArray = [['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']];

        var oneArray = [];
        twoArray.map(function (item ,index) {
            oneArray.push({value: item[0], index: index});
        });
        console.log(oneArray);
        function sortNumber (a, b) {
            return a.value - b.value;
        }
        console.log(oneArray.sort(sortNumber));
        // 此时的oneArray已排好序
        var newTwoArray = [];
        oneArray.map(function (item) {
            newTwoArray.push(twoArray[item.index]);
        });
        console.log(newTwoArray);// 即你要的排序

    Antwort
    0
  • 黄舟

    黄舟2017-06-12 09:29:27

    var groupNum = 3;
    
    var arr1 = [['10','8'],['11','4'],['18','7'],['7','6'],['8','7'],['9','5']];
    
    var arr2 = [];
    
    for (var i = arr1.length; i > 0 ; i -= groupNum) {
      arr2.push(arr1.slice(i - groupNum, i));
    }
    
    console.log(arr2) // [['7','6'],['8','7'],['9','5'],['10','8'],['11','4'],['18','7']]

    Antwort
    0
  • StornierenAntwort