Skip to content

Instantly share code, notes, and snippets.

@montyr75
Last active August 14, 2020 19:48
Show Gist options
  • Save montyr75/1f74cee222fe5b9b3381d30a074a5687 to your computer and use it in GitHub Desktop.
Save montyr75/1f74cee222fe5b9b3381d30a074a5687 to your computer and use it in GitHub Desktop.
Sort by multiple factors.
String meeting(String s) {
final people = s.split(';').map((String name) => Person.fromData(name)).toList();
people.sort();
return people.join();
}
class Person implements Comparable<Person> {
final firstName;
final lastName;
const Person(this.firstName, this.lastName);
factory Person.fromData(String data) {
final splitData = data.toUpperCase().split(':');
return Person(splitData.first, splitData.last);
}
int compareTo(Person other) {
final result = lastName.compareTo(other.lastName);
return result == 0 ? firstName.compareTo(other.firstName) : result;
}
@override
String toString() => "($lastName, $firstName)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment