gpt4 book ai didi

javascript - 二维数组的位置排序

转载 作者:行者123 更新时间:2023-11-29 17:48:16 26 4
gpt4 key购买 nike

<分区>

问题

我有一个二维数组,它实际上是输入到 Google 表格中的数据。它按逻辑排序,由用户定义。

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

Screen Shot

说“按位置”是指“欧洲”排在“美洲”之前,因为用户输入的时间较早。

这是用于测试的示例数组:

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 .

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com