I have 4 companies and each company has more than 10 branches.
If a user chooses a company, I need to show the selected company branch using showDialog
. Till here everything work as expected. My problem is that I cannot sort List<String>
alphabetically.
Any idea how to sort string list (List) alphabetically in Dart?
List<String> _myBranchListName;
Flutter is an application development SDK
Dart is a general-purpose programming language
Flutter application is written in Dart
so:
How to sort A to Z in Flutter List< String > ?
Will be
How to sort A to Z in Dart List< String > ?
A to Z : is called alphabetically so :
How to sort alphabetically in Dart List< String > ?
List< String > : is a list of type string so
How to sort alphabetically in Dart String List?
more
How to sort a string list alphabetically in Dart language?
Code:
main() {
List<String> _myBranchListName= ['k branch', 'a branch', 'f branch'];
_myBranchListName.sort();
print(_myBranchListName);
//[a branch, f branch, k branch]
}
I have a custom list with menas list contain class produt and product contain varable title and now i need to short my list based on title how to do it'''
sort method:
Sorts this list according to the order specified by the compare function.
The default List implementations use Comparable.compare if compare is omitted.
A Comparator may compare objects as equal (return zero),
even if they are distinct objects. The sort function is
not guaranteed to be stable, so distinct objects that
compare as equal may occur in any order in the result
produts.sort((a, b) => a.title.compareTo(b.title));
List Contains method called sort, that function will sort the list in alphabetical order (from a to z).
I created function for you to make the process more clear:
List<String> sort(List<String> _myBranchListName){ // This function take List of strings and return it organized alphabetically
// List<String> _myBranchListName = ["B branch", "C branch" , "A branch"]; // example of input
print(_myBranchListName); // will show the result in the run log
_myBranchListName.sort();
print(_myBranchListName); // will show the result in the run log
return _myBranchListName;
}
Technically you only need _myBranchListName.sort()
to sort the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With