Skip to content

Instantly share code, notes, and snippets.

@tailorvj
Last active November 19, 2020 12:30
Show Gist options
  • Save tailorvj/c86890e350ea84f9c8979a6ddf61aa55 to your computer and use it in GitHub Desktop.
Save tailorvj/c86890e350ea84f9c8979a6ddf61aa55 to your computer and use it in GitHub Desktop.
Example: sorting a list of objects in Dart
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