compareDateTimeNullable method
Compares two nullable DateTimes for sorting purposes.
Null DateTimes are considered to be before non-null DateTimes.
Returns:
0
if both DateTimes are considered equal (both null or representing the same point in time).-1
if this nullable DateTime is considered smaller (comes before) theother
nullable DateTime.1
if this nullable DateTime is considered larger (comes after) theother
nullable DateTime.
Example Usage:
DateTime? dt1 = DateTime.now();
DateTime? dt2 = dt1.add(Duration(days: 1));
DateTime? dt3 = null;
dt1.compareDateTimeNullable(dt2); // -1
dt2.compareDateTimeNullable(dt1); // 1
dt1.compareDateTimeNullable(dt1); // 0
dt3.compareDateTimeNullable(dt1); // -1 (null is considered before non-null)
dt1.compareDateTimeNullable(dt3); // 1 (non-null is considered after null)
dt3.compareDateTimeNullable(null); // 0 (both null are considered equal)
Implementation
int compareDateTimeNullable(DateTime? other) {
if (this == other) {
return 0;
}
if (this == null) {
return -1;
}
if (other == null) {
return 1;
}
return this!.compareTo(other);
}