Home > OS >  How can I sort List<Map<String, dynamic>> by datetime(String)
How can I sort List<Map<String, dynamic>> by datetime(String)

Time:10-24

How can I sort the list of Map the datetime(String) which I declare as String to sort in descending order?

  final List<Map<String, dynamic>> _allUsers = [
    {"name": "Chan Saw Lin", "phone": "0152131113" ,"Clock-In" : "2020-06-30 16:10:05"},
    {"name": "Lee Saw Loy", "phone": "0161231346","Clock-In" : "2020-07-11 15:39:59"},
    {"name": "Khaw Tong Lin", "phone": "0158398109","Clock-In" : "2020-08-19 11:10:18"},
    {"name": "Lim Kok Lin", "phone": "0168279101","Clock-In" : "2020-08-19 11:11:35"},
    {"name": "Low Jun Wei", "phone": "0112731912","Clock-In" : "2020-08-15 13:00:05"},
    {"name": "Yong Weng Kai", "phone": "0172332743","Clock-In" : "2020-07-31 18:10:11"},
    {"name": "Jayden Lee", "phone": "0191236439","Clock-In" : "2020-08-22 08:10:38"},
    {"name": "Kong Kah Yan", "phone": "0111931233","Clock-In" : "2020-07-11 12:00:00"},
    {"name": "Jasmine Lau", "phone": "0162879190","Clock-In" : "2020-08-01 12:10:05"},
    {"name": "Chan Saw Lin", "phone": "016783239","Clock-In" : "2020-08-23 11:59:05"},
  ]; 

CodePudding user response:

Use sort on the list, something like this:

_allUsers.sort((a, b) => (b["Clock-In"] as String).compareTo(a["Clock-In"] as String));

Switch place of a and b to get ascending order.

CodePudding user response:

You can simply sort your list comparing the Check-in data with this code :

_allUsers.sort((a, b) => b['Clock-In'].compareTo(a['Clock-In']));

  • Related