Created
September 3, 2020 23:21
-
-
Save iqan/6cc01ceda5b318714a44e135a8876cb6 to your computer and use it in GitHub Desktop.
sort based on multiple properties
This file contains hidden or 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
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