Skip to content

Instantly share code, notes, and snippets.

@kenyk7
Last active October 13, 2022 03:23
Show Gist options
  • Save kenyk7/dadecd4dd5100fbc1fbe72d6a4a1e8ed to your computer and use it in GitHub Desktop.
Save kenyk7/dadecd4dd5100fbc1fbe72d6a4a1e8ed to your computer and use it in GitHub Desktop.
Dart order list object by property

Dart order list object by property

Created with <3 with dartpad.dev.

class Category {
const Category({
required this.name,
});
final String name;
}
List<Category> categories = [
const Category(name: 'cat3'),
const Category(name: 'cat1'),
const Category(name: 'cat2'),
];
void main() {
categories.sort((a, b) => a.name.compareTo(b.name));
// print result
for (final category in categories) {
print(category.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment