1

This is my List:

myList = [[0.0, 3 Jan 2023], [0.0, 7 Jan 2023], [139.36986081071, 1 Jan 2023], [139.84969328013125, 11 Jan 2023], [97.84468694063244, 11 Jan 2023]];

I would like to sort that list not by the double number but by dateTime, the earlier date first. Of course I don't want the lists inside the List to brake.

Any help appreciate it :)

Thanks!

Biobrolly
  • 113
  • 2
  • 12
  • See my answers to [How do I convert a date/time string to a DateTime object in Dart?](https://stackoverflow.com/a/61394854/) and [Sort a list of objects in Flutter (Dart) by property value](https://stackoverflow.com/a/61673246/). In particular, I would use the `sortWithKey` function that I described to avoid parsing strings into `DateTime` objects an extraneous number of times. – jamesdlin Jan 12 '23 at 16:45

1 Answers1

3

You can use List.sort() with DateTime.compareTo():

myList.sort((a, b) => a[1].compareTo(b[1]));

Using your example data:

var myList = <List<dynamic>>[
  [0.0, DateTime(2023, 01, 03)], 
  [0.0, DateTime(2023, 01, 07)], 
  [139.36986081071, DateTime(2023, 01, 01)], 
  [139.84969328013125, DateTime(2023, 01, 11)], 
  [97.84468694063244, DateTime(2023, 01, 11)]
]; 

main() {
  myList.sort((a, b) => a[1].compareTo(b[1]));
  print(myList);
}

The output is:

[[139.36986081071, 2023-01-01 00:00:00.000], [0, 2023-01-03 00:00:00.000], [0, 2023-01-07 00:00:00.000], [139.84969328013125, 2023-01-11 00:00:00.000], [97.84468694063244, 2023-01-11 00:00:00.000]]
Chuck Batson
  • 2,165
  • 1
  • 17
  • 15
  • Thanks! The problem I encounter is that after the 1 Jan come the 11 Jan and not the 3 Jan. Any ideas how to fix that? For some reason 11 comes before 3. – Biobrolly Jan 12 '23 at 11:18
  • @Biobrolly Are the dates in your list `DateTime` or string? If the latter, please clarify your original question because it says "dateTime." – Chuck Batson Jan 12 '23 at 16:17
  • Thanks for your reply. You are right. Because the DateTime is formatted to look like this, it has become a String. Sorry did not pay attention to that. What can I do about it? Is there a way to sort the String correctly? Or should I find a way to convert it back to DateTime? thanks – Biobrolly Jan 12 '23 at 17:44
  • @Biobrolly I'm not clear what you mean by the `DateTime` "has become a String." If you have `DateTime` objects, then your best bet is to keep them as `DateTime` objects and avoid the conversion to string. – Chuck Batson Jan 12 '23 at 17:54
  • Because I want the DateTime to be displayed as this format: 7 Jan 2023, I did this: formattedDate = DateFormat('d MMM yyyy').format(_dateTime); Doesn't this convert it to a String? and Then I saved this in the database, so I can retrieve it . – Biobrolly Jan 12 '23 at 17:57
  • I fixed it by keeping the DateTime format as suggested to you and making the formattedDate whereEver i needed to display Text. I do not know if it is good practice or not but I am still learning and it helps. Thanks for your help man! – Biobrolly Jan 12 '23 at 18:02
  • 1
    I think you're on the right track. It's best format / convert to string only for display. Think of how values are *stored* as distinct from how they're *displayed*. You can consider storing the values in your database in a numeric form, perhaps seconds or milliseconds since epoch, as an example. – Chuck Batson Jan 12 '23 at 18:05