How can I sort a list of strings in Dart?

后端 未结 5 1992
萌比男神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.

    0 讨论(0)
  • 2020-12-03 07:03

    How I have solved this problem.

    
      List<Product> _dataSavingListProducts = [];
      List<Product> _dataSavingListFavoritesProducts = [];
    
      void _orderDataSavingLists() {
        _dataSavingListProducts.toList().reversed;
        _dataSavingListFavoritesProducts.toList().reversed;
      }
    
    
    
    0 讨论(0)
  • 2020-12-03 07:05

    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.

    0 讨论(0)
  • 2020-12-03 07:06

    Here is the one line code to achieve it.

    fruits.sort((String a, String b)=>a.compareTo(b)); //fruits is of type List<String>
    
    0 讨论(0)
  • 2020-12-03 07:12

    1. A Quick Solution

    Thanks for the question! You can sort a list of Strings like this:

    main() {
      final List<String> fruits = <String>['bananas', 'apples', 'oranges'];
      fruits.sort();
      print(fruits);
    }
    

    The above code prints:

    [apples, bananas, oranges]
    

    2. Slightly more advanced usage

    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:

    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() {
      final List<String> fruits = <String>['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 sort method, which has one optional parameter: a Comparator. A Comparator is a typedef or function alias. In this case, it's an alias for a function that looks like:

    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.

    3. How to do it with a list of custom objects

    Additionally, if you create a list composed of custom objects, you could add the Comparable<T> as a mixin or as inheritance (extends) and then override the compareTo method, in order to recreate the standard behavior of sort() for your list of custom objects. For more info, do check out this other, related StackOverflow answer.

    0 讨论(0)
提交回复
热议问题