Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort a list of strings in Dart?

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.

like image 356
george koller Avatar asked Oct 15 '12 01:10

george koller


People also ask

How do you sort a string list in flutter?

typedef Sort = int Function(dynamic a, dynamic b); typedef SortF = Sort Function(String sortField); SortF alphabetic = (String sortField) => (a, b){ return a[sortField]. toLowerCase(). compareTo(b[sortField]. toLowerCase()); }; SortF number = (String sortField) => (a, b) { return a[sortField].

How do you sort a list of elements in darts?

Pass a custom compare function into list's sort() method with swap the places of the items. We can sort a List of objects in Descending order using reversed property. It returns an Iterable of the objects in the list. Firstly, we extend Comparable abstract class and override compareTo() method, then we call list.

How do I sort lists in flutter?

Using the sort() method helps us to quickly sort a list of numbers in ascending or descending order. Note that this method does not return a new list but manipulates the original list.

What sorting algorithm does Dart use?

Implementing Quick Sort in Dart. Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.


1 Answers

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.

like image 166
Seth Ladd Avatar answered Sep 20 '22 13:09

Seth Ladd