string 如何在 Dart 中对字符串列表进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12888206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I sort a list of strings in Dart?
提问by george koller
I see in the API docs there is a sort()
method on List
, but I'm not clear what it needs for a parameter. The current need is for a very simple straight up alpha comparison.
我在 API 文档中看到在 上有一个sort()
方法List
,但我不清楚它需要什么参数。当前需要进行非常简单的直接 alpha 比较。
回答by Seth Ladd
Thanks for the question! You can sort a list of Strings like this:
感谢提问!您可以像这样对字符串列表进行排序:
main() {
var fruits = ['bananas', 'apples', 'oranges'];
fruits.sort();
print(fruits);
}
The above code prints:
上面的代码打印:
apples, bananas, oranges
Notice that sort()
does not return a value. It sorts the list without creating a new list. If you want to sort and print in the same line, you can use method cascades:
请注意,sort()
它不返回值。它在不创建新列表的情况下对列表进行排序。如果要在同一行中排序和打印,可以使用方法级联:
print(fruits..sort());
For more control, you can define your own comparison logic. Here is an example of sorting the fruits based on price.
如需更多控制,您可以定义自己的比较逻辑。这是根据价格对水果进行分类的示例。
main() {
var fruits = ['bananas', 'apples', 'oranges'];
fruits.sort((a, b) => getPrice(a).compareTo(getPrice(b)));
print(fruits);
}
Let's see what's going on here.
让我们看看这里发生了什么。
A List has a sortmethod, which has one optionalparameter: a Comparator. A Comparator is a typedef
or function alias. In this case, it's an alias for a function that looks like:
List 有一个sort方法,它有一个可选参数:Comparator。Comparator 是一个typedef
或函数别名。在这种情况下,它是一个函数的别名,如下所示:
int Comparator(T a, T b)
From the docs:
从文档:
A Comparator function represents such a total ordering by returning a negative integer if a is smaller than b, zero if a is equal to b, and a positive integer if a is greater than b.
Comparator 函数通过在 a 小于 b 时返回负整数,如果 a 等于 b 返回零,如果 a 大于 b 返回正整数来表示这样的总排序。
回答by Paras khandelwal
Here is the one line code to achieve it.
这是实现它的一行代码。
fruits.sort((String a, String b)=>a.compareTo(b)); //fruits is of type List<String>
回答by JONNALAGADDA Srinivas
To add just one point to Seth's detailed answer, in general, in
仅在赛斯的详细答案中添加一点,一般来说,在
(a, b) => foo(a, b)
passed into sort
, the function foo
should answer an integer result as follows:
传入sort
,该函数foo
应按如下方式回答整数结果:
- if a < b, result should be < 0,
- if a = b, result should be = 0, and
- if a > b, result should be > 0.
- 如果 a < b,结果应该 < 0,
- 如果 a = b,结果应该是 = 0,并且
- 如果 a > b,结果应该 > 0。
For the above law of trichotomy to hold, both a
and b
must be Comparable
s.
对于三分法的上述法律持有,两者a
并b
必须Comparable
秒。
回答by Lasse Nielsen
After today, you should just be able to do list.sort() . The sort method's argument is now optional, and it defaults to a function that calls compareTo on the elements themselves. Since String is Comparable, it should Just Work now.
今天之后,你应该能够执行 list.sort() 。sort 方法的参数现在是可选的,它默认为一个对元素本身调用 compareTo 的函数。由于 String 是 Comparable,它应该立即工作。