Skip to content

Instantly share code, notes, and snippets.

@iqan
Created September 3, 2020 23:21
Show Gist options
  • Save iqan/6cc01ceda5b318714a44e135a8876cb6 to your computer and use it in GitHub Desktop.
Save iqan/6cc01ceda5b318714a44e135a8876cb6 to your computer and use it in GitHub Desktop.
sort based on multiple properties
class someclass {
int quantity;
DateTime mydate;
someclass({this.quantity, this.mydate});
int compareTo(someclass second) {
int res = mydate.compareTo(second.mydate);
if (res == 0) {
return quantity.compareTo(second.quantity);
}
return res;
}
}
void main() {
List<someclass> x = new List<someclass>();
x.add(someclass(quantity: 2, mydate: DateTime.now()));
x.add(someclass(quantity: 1, mydate: DateTime.now().add(Duration(days: 1))));
x.add(someclass(quantity: 3, mydate: DateTime.now()));
x.forEach((y) {
print("---");
print(y.quantity);
print(y.mydate);
print("---");
});
x.sort((a,b) => a.compareTo(b));
x.forEach((y) {
print("---");
print(y.quantity);
print(y.mydate);
print("---");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment