How can I sort a list of strings in Dart?

后端 未结 5 1994
萌比男神i
萌比男神i 2020-12-03 06:18

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 st

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 06:53

    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:

    • if a < b, result should be < 0,
    • if a = b, result should be = 0, and
    • if a > b, result should be > 0.

    For the above law of trichotomy to hold, both a and b must be Comparables.

提交回复
热议问题