3

Problème

J'ai un tableau 2d, qui est en fait des données saisies dans Google Sheet. Il est trié par logique, défini par un utilisateur.Type de position de 2d Tableau

L'objectif est d'entrer de nouvelles lignes à la fin de cette table, puis de les trier par position.

enter image description here

Dire « par la position » je veux dire « l'Europe » va avant « l'Amérique » parce qu'un utilisateur a entré plus tôt.

est ici tableau d'échantillons pour les tests:

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'] 
    ]; 

Et correct tableau résultant est:

/* 
    [ [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] 
    ] 
*/ 

Je veux utiliser un script pour cela.

Ma solution

J'ai fait ma propre version d'un script, s'il vous plaît voir ici:

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

Comment l'algorithme fonctionne

L'algorithme trouve des groupes à partir de la première rangée: Terre> Europe> Grande-Bretagne. Ensuite, il essaie de trouver une correspondance pour ces groupes dans les entrées ultérieures. J'ai aussi pensé à assigner des index plus élevés aux entrées précédentes.

Question

La question: y at-il de meilleures approches:

  1. moins de code pour accomplir la même tâche
  2. Plus façon générale pour trier un tableau par la position
  3. besoin assez de solution rapide, parce que je vais utiliser le code en feuilles et il a limits on script time.
+1

@Luca, j'ai édité la question et limité d'identifier une réponse adéquate. Pourriez-vous s'il vous plaît le rouvrir. J'ai la bonne réponse et espère que ma question serait utile pour les autres utilisateurs –

Répondre

3

Vous pouvez utiliser sorting with map, où chaque groupe obtient le premier index trouvé pour trier le groupe.

Ensuite, prenez le dernier élément pour mapper la matrice.

Il fonctionne avec une table de hachage imbriquée pour les groupes, comme

{ 
    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 
       } 
      } 
     } 
    } 
} 

où chaque propriété _ désigne le premier indice du groupe.

Le tableau temporaire pour le tri ressemble ce,

// 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 ] // --------+ 
] 

Il est récupéré pour trier le tableau temporaire.

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

Merci! Ce code est beau, mais il me donne un ordre alphabétique, pas positionnel. –

+0

ce qui signifie positionnel dans ce cas? –

+0

Cela signifie que "Europe" va avant "Amérique" parce qu'un utilisateur y est entré plus tôt. –