Last active
November 19, 2020 12:30
-
-
Save tailorvj/c86890e350ea84f9c8979a6ddf61aa55 to your computer and use it in GitHub Desktop.
Example: sorting a list of objects in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
List<FullName> fullNamesList = [FullName('Abe','Lincoln'), FullName('Babe', 'Ruth'), FullName('Carry', 'Bradshaw'),]; | |
print ('fullNamesList:\n${fullNamesList.toString()}\n'); | |
fullNamesList.sort((a,b) => a.lastName.compareTo(b.lastName)); | |
print ('fullNamesList after sort by lastName:\n$fullNamesList\n'); | |
fullNamesList.sort((a,b) => a.firstName.compareTo(b.firstName)); | |
print ('fullNamesList after sort by firstName:\n$fullNamesList\n'); | |
} | |
class FullName{ | |
String firstName; | |
String lastName; | |
FullName(this.firstName, this.lastName ); | |
toString(){ | |
return '$firstName $lastName'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment