【问题标题】:JavaScript - How to sort an array of array by number strings?JavaScript - 如何按数字字符串对数组进行排序?
【发布时间】:2017-07-13 04:15:15
【问题描述】:

我收到一个这样的数组:

[
    [{"name":"one","team":"------","checked":false,"position":"-"},
     {"name":"two","team":"------","checked":false,"position":"1"}],
    [{"name":"three","team":"------","checked":false,"position":"3"},
     {"name":"four","team":"------","checked":false,"position":"7"}]
]

我希望最终的输出是这样的:

[
    [{"name":"two","team":"------","checked":false,"position":"1"},
     {"name":"four","team":"------","checked":false,"position":"7"}],
    [{"name":"three","team":"------","checked":false,"position":"3"},
     {"name":"one","team":"------","checked":false,"position":"-"}]
]

表示数组按照第一个子数组中的最大位置值和最小位置值排序,然后是第二个子数组中的第二大位置值和第二最小位置值。如果位置有“-”,则在字符串编号之后。

到目前为止,我已经用 lodash 尝试过这个:

var mergedArray = vm.roundOf4[0].concat(vm.roundOf4[1]);
var count = _.countBy(mergedArray, {position: '-'});

var ascArray = _.cloneDeep(mergedArray);
var descArray = _.cloneDeep(mergedArray);

_.sortByOrder(ascArray, 'position', 'asc');
_.sortByOrder(descArray, 'position', 'desc');

_.times(count.true, ascArray.push(ascArray.shift()));

vm.roundOf4.some(function (array, i) {
    array.some(function (object, j) {

        if (j === 0) {
            vm.roundOf4[i][j] = ascArray[i];

        } else if (j === 1) {
            vm.roundOf4[i][j] = descArray[i];
        }

    });
});

但是升序排序函数没有正确排序位置值,所以它给出了随机排序。不知道是不是因为position是一个字符串值。

我应该改用数字吗?

【问题讨论】:

  • 1- 在同一个子数组中,但在您想要的输出中它们是分开的。这背后的逻辑是什么?
  • 没有逻辑,只是“-”还没有设置。它可以是另一个数字,但因为它尚未设置,所以它留在最后,这同样适用于任何对象。
  • 不说-,我说的是1- 在排序之后是如何分开的! (之前:他们在同一个子数组中,之后:每个人都去了不同的子数组)! 73 相同!
  • 173- 在我看来别有一番风味!
  • @ibrahimmahrir 该数组按第一个数组中的最大位置值和最小位置值排序,然后是第二个数组中的第二大位置值和第二最小位置值。带“-”的位置在字符串数字之后。

标签: javascript arrays json sorting lodash


【解决方案1】:

// get an array of object an return the min index or the max index depending on the parameter max (true => max index, false => min index)
function minMax(arr, max) {
  var index = 0;                                             // assuming the index 0 is the min or max
  for (var i = 1; i < arr.length; i++) {                     // for each other object in the array
    if(arr[index].position == '-')                           // if the position of the object at index index is '-' then assume the index is this i (before starting the comparison, find an item which have a number for its position property)
      index = i;
    else if ((arr[i].position > arr[index].position) == max) // now that the object at index index has a valid position property, make the comparison depending on the argument max
      index = i;
  }
  return index;
}

// take an two dimensional array and rearrange it
function reArrange(arr) {
  var simple = arr.reduce(function(acc, a) {                 // first let's make the two dimensional array into a one dimensional array
    return acc.concat(a);
  }, []);
  
  var result = [];                                           // the result array
  while(simple.length) {                                     // while there is element in the one dimensional array
    var min = minMax(simple, false);                         // get the min index
    var max = minMax(simple, true);                          // get the max
    result.push([                                            // push the items at the min and max indexes as a sub-array of the result array
      simple.splice(min, 1)[0],                              // splice to remove it from simple array as well
      simple.splice((max < min)? max: max - 1, 1)[0]         // if max index is after min index then substract one because the above splice shrank the array 
    ]);
  }
  return result;
}



var arr = [
    [{"name":"one","team":"------","checked":false,"position":"-"},
     {"name":"two","team":"------","checked":false,"position":"1"}],
    [{"name":"three","team":"------","checked":false,"position":"3"},
     {"name":"four","team":"------","checked":false,"position":"7"}]
];

console.log(reArrange(arr));

【讨论】:

    【解决方案2】:

    您可以对内部数组进行排序,然后分配给结果并再次排序。

    var data = [[{ "name": "one", "team": "------", "checked": false, "position": "-" }, { "name": "two", "team": "------", "checked": false, "position": "1" }], [{ "name": "three", "team": "------", "checked": false, "position": "3" }, { "name": "four", "team": "------", "checked": false, "position": "7" }]],
        result = [];
    
    data.forEach(function (a) {
        a.sort(function (a, b) { return (+b.position || Number.MIN_VALUE) - (+a.position || Number.MIN_VALUE); });
    });
    
    data.forEach(function (a, i) {
        a.forEach(function (b, j) {
            result[j] = result[j] || [];
            result[j][i] = b;
        });
    });
    
    result.forEach(function (a) {
        a.sort(function (a, b) { return (+a.position || Number.MAX_VALUE) - (+b.position || Number.MAX_VALUE); });
    });
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 2014-07-10
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2019-03-16
      相关资源
      最近更新 更多