[英]Sort a List in Dart based on how another List was sorted
I am trying to sort a list with reference to another list.我正在尝试参考另一个列表对列表进行排序。 Consider this example,
考虑这个例子,
List<String> a = ["a", "b", "c"];
List<int> b = [2, 3, 1];
Now, I want the result to be like this,现在,我希望结果是这样的,
a = ["c", "a", "b"];
b = [1, 2, 3];
How can I achieve this result?我怎样才能达到这个结果?
You can achieve this by create a Map
that holds the mappings for the letters to your numbers, ie 2
has a
assigned to it, 3
has b
etc. 您可以通过创建一个
Map
来实现此目的,该Map
包含字母到数字的映射,即2
分配了a
字母, 3
分配了b
等。
Now you can sort your list normally and after that recreate your other list based on the mappings that you stored before sorting: 现在,您可以正常地对列表进行排序,然后根据排序之前存储的映射重新创建其他列表:
main() {
List<String> letters = ["a", "b", "c"];
final List<int> numbers = [2, 3, 1];
final Map<int, String> mappings = {
for (int i = 0; i < numbers.length; i++)
numbers[i]: letters[i]
};
numbers.sort();
letters = [
for (int number in numbers) mappings[number]
];
print(letters); // [c, a, b]
print(numbers); // [1, 2, 3]
}
I used collection for for this example. 在此示例中,我使用了集合 。 This syntax for collection literals was introduced in Dart
2.3
. Dart
2.3
中引入了用于集合文字的语法。
Learn more . 了解更多 。
You can easily turn this about and map the numbers to your letters instead. 您可以轻松地将其转过来,然后将数字映射到您的字母。 That would look like this:
看起来像这样:
main() {
final List<String> letters = ["a", "b", "c"];
List<int> numbers = [2, 3, 1];
final Map<String, int> mapping = {
for (int i = 0; i < letters.length; i++)
letters[i]: numbers[i]
};
letters.sort((a, b) => b.compareTo(a));
numbers = [
for (String letter in letters) mapping[letter]
];
print(letters); // [c, b, a]
print(numbers); // [1, 3, 2]
}
Note that I sorted the letters in a descending order here by turning about the compareTo
as well (by default a.compareTo(b)
is used by List.sort
). 请注意 ,我排序的字母按降序这里转动有关
compareTo
以及(默认a.compareTo(b)
所使用的List.sort
)。
If your List
s have duplicate elements, you won't be able to use a Map
.如果您的
List
有重复的元素,您将无法使用Map
。 I'd instead use package:tuple
and then use the same approach of combining the two List
s into a single List<Tuple2>
, sorting that, and extracting the values:我改为使用
package:tuple
,然后使用相同的方法将两个List
组合成一个List<Tuple2>
,对其进行排序,然后提取值:
import 'package:tuple/tuple.dart';
void main() {
var a = ["a", "b", "c", "d"];
var b = [2, 3, 1, 1];
var combined = <Tuple2<int, String>>[
for (var i = 0; i < b.length; i += 1) Tuple2(b[i], a[i]),
];
combined.sort((tuple1, tuple2) => tuple1.item1.compareTo(tuple2.item1));
for (var i = 0; i < combined.length; i += 1) {
a[i] = combined[i].item2;
b[i] = combined[i].item1;
}
print(a); // Prints: [c, d, a, b]
print(b); // Prints: [1, 1, 2, 3]
}
I use this way to solve the problem:我用这种方式来解决问题:
List<String> strings = ["a", "b", "c", "c"];
List<int> numbers = [2, 3, 1, 0];
List<int> storeNumbers = List.from(numbers);
List<int> storeIndex = [];
//sorting numbers list
numbers.sort((a, b) => a.compareTo(b));
//get the index of numbers list elements before the sorting
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < storeNumbers.length; j++) {
if (numbers[i] == storeNumbers[j]) {
storeIndex.add(j);
}
}
}
//generate a new string list based on the indexes we get
strings =
List.generate(strings.length, (index) => strings[storeIndex[index]]);
print(strings);//output:[c, c, a, b]
print(numbers);//output:[0, 1, 2, 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.