Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript多级数组排序基于键,但在第一个键中具有逻辑结构_Javascript_Arrays_Sorting_Directory_Tree - Fatal编程技术网

Javascript多级数组排序基于键,但在第一个键中具有逻辑结构

Javascript多级数组排序基于键,但在第一个键中具有逻辑结构,javascript,arrays,sorting,directory,tree,Javascript,Arrays,Sorting,Directory,Tree,我有一些特殊的问题。我有很多这样的记录: let a = [ [[1,2,3], ... , b], [[1,2,5], ... , a] [[1,2], ... , a], [[1], ... , z] [[1,2,5], ... , a], [[1,2,3,5], ... , b], [[1,2,3,4], ... , a], [[1,3], ... , a], [[1,3,3], ... , b], [[1,6], ... , a], [[1,3

我有一些特殊的问题。我有很多这样的记录:

let a = [
  [[1,2,3], ... , b],
  [[1,2,5], ... , a]
  [[1,2], ... , a],
  [[1], ... , z]
  [[1,2,5], ... , a],
  [[1,2,3,5], ... , b],
  [[1,2,3,4], ... , a],
  [[1,3], ... , a],
  [[1,3,3], ... , b],
  [[1,6], ... , a],
  [[1,3,5], ... , d],
  [[1,2,4], ... , b],
  [[1,3,1], ... , a],
  [[1,3,2], ... , c],
]
let b = [
  [[1], ... , z],
  [[1,2], ... , a],
  [[1,2,5], ... , a]
  [[1,2,3], ... , b],
  [[1,2,3,4], ... , a],
  [[1,2,3,5], ... , b],
  [[1,2,4], ... , b],
  [[1,2,6], ... , d],
  [[1,3], ... , a],
  [[1,3,1], ... , a],
  [[1,3,3], ... , b],
  [[1,3,2], ... , c],
  [[1,3,5], ... , d],
  [[1,6], ... , a],
]
所以我有一个数组(级别0),其中包含一些数据的数组(级别1)和路径部分的数组(级别2)

现在,我必须按照例如键2对数组级别0进行排序,但是路径仍然应该构建逻辑树或类似的东西

因此,如果我按最后一个键排序,我应该得到如下结果:

let a = [
  [[1,2,3], ... , b],
  [[1,2,5], ... , a]
  [[1,2], ... , a],
  [[1], ... , z]
  [[1,2,5], ... , a],
  [[1,2,3,5], ... , b],
  [[1,2,3,4], ... , a],
  [[1,3], ... , a],
  [[1,3,3], ... , b],
  [[1,6], ... , a],
  [[1,3,5], ... , d],
  [[1,2,4], ... , b],
  [[1,3,1], ... , a],
  [[1,3,2], ... , c],
]
let b = [
  [[1], ... , z],
  [[1,2], ... , a],
  [[1,2,5], ... , a]
  [[1,2,3], ... , b],
  [[1,2,3,4], ... , a],
  [[1,2,3,5], ... , b],
  [[1,2,4], ... , b],
  [[1,2,6], ... , d],
  [[1,3], ... , a],
  [[1,3,1], ... , a],
  [[1,3,3], ... , b],
  [[1,3,2], ... , c],
  [[1,3,5], ... , d],
  [[1,6], ... , a],
]
解决了

我发现最好最简单的解决方案来自lodash库。所以我可以先按第一列排序,然后按所选键排序——这样结构就变得合乎逻辑,排序的只是“文件夹中的文件”

代码如下:

let a = [
  [[1,2,3], ... , b],
  [[1,2,5], ... , a]
  [[1,2], ... , a],
  [[1], ... , z]
  [[1,2,5], ... , a],
  [[1,2,3,5], ... , b],
  [[1,2,3,4], ... , a],
  [[1,3], ... , a],
  [[1,3,3], ... , b],
  [[1,6], ... , a],
  [[1,3,5], ... , d],
  [[1,2,4], ... , b],
  [[1,3,1], ... , a],
  [[1,3,2], ... , c],
]

function TreeStructure(arr, column, dir) {
  // Create array duplicate to avoid changes on base array
  let a = [...arr];
  // flip sort direction based on passed value
  // Magic of _Lodash - firstly, sort by the tree ignoring the last item of the tree path,
  // Then sort by the selected key
  a = _.sortBy(a, [
    function (item) {
  // first - sort by the first array, but with ignoring the last item
  // as I mentioned - it's representation of folder tree
  // so the last element is the file
      const value = item[0].length > 1 ? item[0].slice(0, item[0].length - 1).join("-") : item[0].length;
      return value;
    },
    function (item) {
  // Then, sort by the selected column
  // To prevent errors if some objects or arrays comes in the column I convert them to string
  // And to avoid specyfic JS sorting, I convert it to lowercase.
      const value = item[column].toString().toLowerCase();
      return dir === "asc" ? value : -value;
    },
  ]);

return a;
}

最短的方法是使用由分隔符连接的所需数组的选项和字符串

此方法尊重具有多个数字的值并自然排序

为了对不同的列进行排序,您可以为每个排序方法和一个具有列键并返回一个函数作为排序回调的对象获取函数

const
sortPath=col=>(a,b)=>a[col]。连接('-')。本地比较(
b[col].连接('-'),
未定义,
{numeric:true,灵敏度:'base'}
),
sortAlpha=col=>((a,b)=>a[col].localeCompare(b[col]),
cols={path:sortPath(0),title:sortAlpha(1)},
[1、2、2、3 3],[[1、2、3、3、5],“a”,[1、2],[1、2],“a”,[1、[1、2、2、[1[1、2、2、2、3、3],[[1、2、2、2、3、5],[[1、2、2、5],[1、2、[1、2],[1、2],[1、2],[1、[1、[1、[1、2、[1、2],[1、[1、2],[1、[1、2、[1、[1、2、[1、2、[1、2、[1、3]、[1、3]、[1、3、[1、3、3、3、3、3、3、3、3、3、3、3、3、[3、3、3、3、3],[3、[1、3、[3、']];
array.sort(cols.path);
forEach(a=>console.log(JSON.stringify(a));
console.log('------------');
array.sort(cols.title);
forEach(a=>console.log(JSON.stringify(a))

。作为控制台包装{max height:100%!important;top:0;}
它几乎完美,但我必须能够选择按哪个键排序。例如,如果我的值表示:[[paths array]、“title”、“size”、“status”、“content”],我需要选择以相同的方式排序,但按第二个或第三个键排序。