3

问题

我有一个2d数组,它实际上是输入到Google Sheet中的数据。它按用户定义的逻辑进行排序。位置排序的2d数组

目标是在此表末尾输入新行,然后按位置对其进行排序。

enter image description here

说“的位置”我的意思是“欧洲”变“美”之前,因为用户已经较早进入它。

下面是测试样品阵列:

var data = 
    [ 
    ['Earth', 'Europe', 'Britain', 'London'], 
    ['Earth', 'Europe', 'Britain', 'Manchester'], 
    ['Earth', 'Europe', 'Britain', 'Liverpool'], 
    ['Earth', 'Europe', 'France', 'Paris'], 
    ['Earth', 'Europe', 'France', 'Lion'], 
    ['Earth', 'Europe', 'Italy', 'Rome'], 
    ['Earth', 'Europe', 'Italy', 'Milan'], 
    ['Earth', 'Europe', 'Greece', 'Athenes'], 
    ['Earth', 'Asia', 'China', 'Pekin'], 
    ['Earth', 'Africa', 'Algeria', 'Algiers'], 
    ['Earth', 'America', 'USA', 'Dallas'], 
    ['Earth', 'America', 'USA', 'New York'], 
    ['Earth', 'America', 'USA', 'Chicago'], 
    ['Tatooine', 'Yulab', 'Putesh', 'ASU'], 
    ['Tatooine', 'Yulab', 'Putesh', 'Niatirb'], 
    ['Tatooine', 'Yulab', 'Zalip', 'Duantan'], 
    ['Tatooine', 'Asia', 'Solo', 'Lion'], 
    ['Tatooine', 'Asia', 'Solo', 'To'], 
    ['Earth', 'America', 'USA', 'San Francisco'], 
    ['Tatooine', 'Yulab', 'Koko', 'Traiwau'], 
    ['Venus', 'Yoo', 'Van', 'Derzar'], 
    ['Tatooine', 'Chendoo', 'org', 'Eccel'] 
    ]; 

,正确的结果数组是:

/* 
    [ [Earth, Europe, Britain, London], 
    [Earth, Europe, Britain, Manchester], 
    [Earth, Europe, Britain, Liverpool], 
    [Earth, Europe, France, Paris], 
    [Earth, Europe, France, Lion], 
    [Earth, Europe, Italy, Rome], 
    [Earth, Europe, Italy, Milan], 
    [Earth, Europe, Greece, Athenes], 
    [Earth, Asia, China, Pekin], 
    [Earth, Africa, Algeria, Algiers], 
    [Earth, America, USA, Dallas], 
    [Earth, America, USA, New York], 
    [Earth, America, USA, Chicago], 
    [Earth, America, USA, San Francisco], 
    [Tatooine, Yulab, Putesh, ASU], 
    [Tatooine, Yulab, Putesh, Niatirb], 
    [Tatooine, Yulab, Zalip, Duantan], 
    [Tatooine, Yulab, Koko, Traiwau], 
    [Tatooine, Asia, Solo, Lion], 
    [Tatooine, Asia, Solo, To], 
    [Tatooine, Chendoo, org, Eccel], 
    [Venus, Yoo, Van, Derzar] 
    ] 
*/ 

我想用一个脚本这一点。

我的解决方案

我做了我自己的脚本的版本,请在这里看到:

https://github.com/Max-Makhrov/positional-sorting/blob/master/main.js

算法如何工作

算法找到组从第一次开始排:地球>欧洲>英国。然后它会尝试在稍后的条目中为这些组找到一个匹配项。

我也想过将更高的索引分配给更早的条目。

问题

的问题:是否有更好的方法:

  1. 更少的代码来完成相同的任务
  2. 更普遍的方式来排序位置的阵列
  3. 需要足够快的解决方案,因为我将使用表单中的代码,并且它有limits on script time
+1

@Luca,我编辑的问题,限制它,以确定适当的答案。你能重新打开它吗?我已经得到了正确的答案,并希望我的问题对其他用户有所帮助 –

回答

3

您可以使用sorting with map,其中每个组获得第一个找到的排序组索引。

稍后将最后一项映射回数组。

它与这些组嵌套哈希表,像

{ 
    Earth: { 
     _: 0, 
     Europe: { 
      _: 0, 
      Britain: { 
       _: 0, 
       London: { 
        _: 0 
       }, 
       Manchester: { 
        _: 1 
       }, 
       Liverpool: { 
        _: 2 
       } 
      }, 
      // ... 
     }, 
     // ... 
     America: { 
      _: 10, 
      USA: { 
       _: 10, 
       Dallas: { 
        _: 10 
       }, 
       "New York": { 
        _: 11 
       }, 
       Chicago: { 
        _: 12 
       }, 
       "San Francisco": { 
        _: 18 
       } 
      } 
     } 
    } 
} 

,其中每个属性_表示基团的第一索引。

的临时数组进行排序看起来像这样,

// index of group 
//  index of group 
//   index of group 
//    own index 
[ 
    [ 0, 0, 0, 0 ], 
    [ 0, 0, 0, 1 ], 
    [ 0, 0, 0, 2 ], 
    [ 0, 0, 3, 3 ], 
    [ 0, 0, 3, 4 ], 
    [ 0, 0, 5, 5 ], 
    [ 0, 0, 5, 6 ], 
    [ 0, 0, 7, 7 ], 
    [ 0, 8, 8, 8 ], 
    [ 0, 9, 9, 9 ], 
    [ 0, 10, 10, 10 ], 
    [ 0, 10, 10, 11 ], 
    [ 0, 10, 10, 12 ], // /_  moving between 
    [ 13, 13, 13, 13 ], // \ |  both items 
    [ 13, 13, 13, 14 ], // | 
    [ 13, 13, 15, 15 ], // |/_ 
    [ 13, 16, 16, 16 ], // |\ | 
    [ 13, 16, 16, 17 ], // | |/_ 
    [ 0, 10, 10, 18 ], // --+ |\ | 
    [ 13, 13, 19, 19 ], // -----+ | 
    [ 20, 20, 20, 20 ], //   | 
    [ 13, 21, 21, 21 ] // --------+ 
] 

这取用于分拣临时数组。

var data = [['Earth', 'Europe', 'Britain', 'London'], ['Earth', 'Europe', 'Britain', 'Manchester'], ['Earth', 'Europe', 'Britain', 'Liverpool'], ['Earth', 'Europe', 'France', 'Paris'], ['Earth', 'Europe', 'France', 'Lion'], ['Earth', 'Europe', 'Italy', 'Rome'], ['Earth', 'Europe', 'Italy', 'Milan'], ['Earth', 'Europe', 'Greece', 'Athenes'], ['Earth', 'Asia', 'China', 'Pekin'], ['Earth', 'Africa', 'Algeria', 'Algiers'], ['Earth', 'America', 'USA', 'Dallas'], ['Earth', 'America', 'USA', 'New York'], ['Earth', 'America', 'USA', 'Chicago'], ['Tatooine', 'Yulab', 'Putesh', 'ASU'], ['Tatooine', 'Yulab', 'Putesh', 'Niatirb'], ['Tatooine', 'Yulab', 'Zalip', 'Duantan'], ['Tatooine', 'Asia', 'Solo', 'Lion'], ['Tatooine', 'Asia', 'Solo', 'To'], ['Earth', 'America', 'USA', 'San Francisco'], ['Tatooine', 'Yulab', 'Koko', 'Traiwau'], ['Venus', 'Yoo', 'Van', 'Derzar'], ['Tatooine', 'Chendoo', 'org', 'Eccel']], 
 
    hash = Object.create(null), 
 
    result = data 
 
     .map(function (a, i) { 
 
      var temp = hash; 
 
      return a.map(function (k) { 
 
       temp[k] = temp[k] || { _: i }; 
 
       temp = temp[k]; 
 
       return temp._; 
 
      }); 
 
     }) 
 
     .sort(function (a, b) { 
 
      var value; 
 
      a.some(function (v, i) { 
 
       return value = v - b[i]; 
 
      }); 
 
      return value; 
 
     }) 
 
     .map(function (indices) { 
 
      return data[indices[indices.length - 1]]; 
 
     }); 
 

 
console.log(result.map(function (a) { return a.join(', '); }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

谢谢!这段代码很漂亮,但它给了我一个字母顺序,而不是位置。 –

+0

这种情况下的位置是什么意思? –

+0

这意味着“欧洲”在“美国”之前,因为用户之前输入了它。 –