javascript - 我可以使用嵌套排序()对嵌套数组进行排序吗?

标签 javascript arrays loops sorting

这应该是输入数组

var a = [2,1,3,4,1,[4,6,2,4],2,4,1];

对于输出我有两种情况:-(内部数组的索引没有改变)

a = [1,1,2,3,4,[2,4,4,6],1,2,4]

a = [1,1,1,2,2,[2,4,4,6],3,4,4]

这就是我正在尝试使用的:-

a.sort(function(a,b){
  if(b instanceof Array){
    b.sort();
  }
})

最佳答案

Array.sort() 不是为处理部分数组而构建的,这是您在您的情况下需要的,但是我们可以通过预处理数据(用附加信息包装它)然后排序来解决这个问题结束,提取原始值:

案例一:对数组之间的部分进行排序
[2,1,3,4,1,[4,6,2,4],2,4,1] -> [1,1,2,3,4, [2,4,4,6],1,2,4]

function sort1(arr){
    //I add an artificial "property" of to the values, to "describe" the groups, and to be able to sort by
    //each Array is it's own group (so they stay in order), and the values in between share the same group
    var group = 0, 
        isArray = false;

    //an intermediate Array holding all the information (in order) to either apply it to the current Array, or to return (map) it as a new Array
    var intermediate = arr.map(function(v,i){
        //last value was an Array, this is the first value after an Array, start a new group
        if(isArray) ++group;    

        if(isArray = Array.isArray(v)){ //update isArray
            v = sort1(v);               //recursive sorting
            ++group;                    //the last group just ended here
        }

        //return a composition, that contains all the data I need to sort by
        return {
            group: group,
            value: v
        }
    }).sort(function(a, b){
        //forst sort by group, and (only) if two values share the same group, sort by the original value
        return a.group - b.group || a.value - b.value
    });

    //apply data to current Array
    intermediate.forEach(function(obj, i){ arr[i] = obj.value });
    return arr;

    //return new Array
    //return intermediate.map(function(obj){ return obj.value });
}

案例 2:将数组视为第一个值
[2,1,3,4,1,[4,6,2,4],2,4,1] -> [1,1,1,2,2, [2,4,4,6],3,4,4]

function sort2(arr){
    //an utility to fetch the first non-array value recursively
    function _value(v){ 
        while(Array.isArray(v)) v = v[0];
        return v;
    }

    var intermediate = arr.map(function(v, i){
        if(Array.isArray(v)) v = sort2(v);
        return {
            index: i,
            value: v,
            sortingValue: _value(v)
        }
    }).sort(function(a, b){
        return a.sortingValue - b.sortingValue || a.index - b.index;
    });

    //apply data to current Array
    intermediate.forEach(function(obj, i){ arr[i] = obj.value });
    return arr;

    //return new Array
    //return intermediate.map(function(obj){ return obj.value });
}

关于javascript - 我可以使用嵌套排序()对嵌套数组进行排序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38552181/

相关文章:

javascript - 如何在所有图像调整大小后触发调整大小事件

Java - Arrays.sort 返回 0

javascript - 创建一个函数,它接受一个对象数组并根据条件返回一个新数组

javascript - Angular - 如何优化代码以缩短加载时间?当前加载时间为 2.45 秒

javascript - 当我将我的代码运行到 Repl.it 软件中时,它无法识别我的任何变量

javascript - 在循环中使用 javascript 闭包作为上下文

javascript - 将 Prop 传递给 vue2 组件

c - if 语句似乎永远不会失败

javascript - "Stop running this script"确认框 - 如何获取有关脚本的更多详细信息?

javascript - 如何将两个数组的值获取到同一个html javascript