I am fetching a list of orders and storing the objects in a List. The object has a property called String deliveryTime
and the times are in 'hh:mm a'
format. I want to sort the list by the deliveryTime of the objects in Ascending order. I created a list of String that has the deliveryTime only and used bubble sort to sort them in ASCENDING order. But I am having trouble sorting the entire list of objects in that order.
NOTE: The list of object has some null
& "ASAP"
as their deliveryTime
value.
Here's the incomplete code:
List<OnlineDeliveryOrder> getSortedOrdersList(
List<OnlineDeliveryOrder> orderList,
) {
List<OnlineDeliveryOrder> tempOrderList = [];
List<DateTime> sortedTimeList = [];
print("List Length before Sort: " + orderList.length.toString());
orderList.forEach((OnlineDeliveryOrder order) {
if (order.deliveryTime != null && order.deliveryTime != "ASAP")
sortedTimeList.add(DateFormat('hh:mm a').parse(order.deliveryTime));
});
sortedTimeList =
sortedTimeList.toSet().toList(); //Taking the unique times only
//Sorting times in asc order using bubble sort algorithm
bool sorted = false;
while (!sorted) {
sorted = true;
for (int i = 0; i < sortedTimeList.length - 1; i++) {
DateTime tempTime;
if (sortedTimeList[i].compareTo(sortedTimeList[i + 1]) == 1) {
// dt1.compareTo(dt2) == 1 if dt1 is a later date than dt2
tempTime = sortedTimeList[i];
sortedTimeList[i] = sortedTimeList[i + 1];
sortedTimeList[i + 1] = tempTime;
sorted = false;
}
}
}
// HOW DO I SORT THE ENTIRE LIST
print("List Length after Sort: " + tempOrderList.length.toString());
// String time = DateFormat('hh:mm a').format(element);
return tempOrderList;
}
Can anyone please guide me on how can I return the sorted list?
Why do you implement a sorting algorithm with O(n^2) time complexity by your own? You can use
List<OnlineDeliveryOrder> getSortedOrdersList(List<OnlineDeliveryOrder> orderList){
var format = DateFormat('hh:mm a');
return List.of(orderList)..sort((a,b){
if(a.deliveryTime == null) return 1;
if(b.deliveryTime == null) return -1;
if(a.deliveryTime == 'ASAP') return 1;
if(b.deliveryTime == 'ASAP') return -1;
return format.parse(a.deliveryTime).compareTo(format.parse(b.deliveryTime));
});
}
This way, first all objects come with valid date, then with 'ASAP'
and then with null
as deliveryTime
.
Or if you want it even more efficient without any package:
class SortObject extends Comparable<SortObject>{
static final DateFormat format = DateFormat('hh:mm a');
final OnlineDeliveryOrder order;
final DateTime? deliveryTime;
SortObject(this.order):this.deliveryTime=order.deliveryTime==null||order.deliveryTime=='ASAP'?null:format.parse(order.deliveryTime);
int compareTo(SortObject other){
if(order.deliveryTime == null) return 1;
if(other.order.deliveryTime == null) return -1;
if(order.deliveryTime == 'ASAP') return 1;
if(other.order.deliveryTime == 'ASAP') return -1;
return deliveryTime!.compareTo(other.deliveryTime!);
}
}
List<OnlineDeliveryOrder> getSortedOrdersList(List<OnlineDeliveryOrder> orderList){
return (orderList.map((a)=>SortObject(a)).toList()..sort((a,b){
return a.compareTo(b);
})).map((a)=>a.order).toList();
}
Or did I get the question wrong? If there are errors in the code, I apologize. I just wrote it down.
I would recommend you to add in the OnlineDeliveryOrder
class another attribute that stores the DateTime
in addition to deliveryTime
which does that as a String
. Or you can store the time only as DateTime
and not as String
at all. If you receive the data as JSON, this is very easy to do. (This way you wouldn't need an extra class or a GroupedList
).