Hello i am trying to sort a map in flutter and i have encountered several methods but none have helped so far.
{ 11-Mittwoch: 07:20:58-07:21:03-23:44:37-00:00:00, 15-Sonntag: 11:11:02-11:11:32-00:00:29-00:00:00, 10-Dienstag: 07:36:20-07:20:53-23:44:32-00:00:00, 28-Samstag: 15:32:18-15:32:21-00:00:01-00:00:01, 30-Montag: 08:25:50-16:28:57-08:03:06-00:00:00, 1-Sonntag: 20:39:11-20:39:33-00:00:15-00:00:06, 3-Dienstag: 17:30:14-17:30:19-00:00:04-00:00:00, 18-Mittwoch: 07:54:51-17:38:01-09:43:09-00:00:00, 5-Donnerstag: 08:17:11-22:07:50-03:45:09-10:05:28, 17-Dienstag: 07:00M-15:20M-7:00M-1:00M, 9-Montag: 18:10:28-18:10:32-00:00:04-00:00:00, 6-Freitag: 07:36:54-14:56:13-07:19:19-00:00:00, 2-Montag: 08:28:39-15:44:11-04:21:33-01:05:34}
as you can see from the code above the keys are a combination of date and day seperated by (-). the approach that i need is how to order this type of map in an ascending order instead of starting from the eleventh day of the month.
i tried using this code:
var mapEntries = allData.entries.toList()
..sort((a, b) => a.key.replaceAll(" ", "").split("-")[0].compareTo(b.key.replaceAll(" ", "").split("-")[0])),
allData
..clear()
..addEntries(mapEntries),
but this is the output i get:
{ 1-Sonntag: 20:39:11-20:39:33-00:00:15-00:00:06, 10-Dienstag: 07:36:20-07:20:53-23:44:32-00:00:00, 11-Mittwoch: 07:20:58-07:21:03-23:44:37-00:00:00, 15-Sonntag: 11:11:02-11:11:32-00:00:29-00:00:00, 17-Dienstag: 07:00M-15:20M-7:00M-1:00M, 18-Mittwoch: 07:54:51-17:38:01-09:43:09-00:00:00, 2-Montag: 08:28:39-15:44:11-04:21:33-01:05:34, 28-Samstag: 15:32:18-15:32:21-00:00:01-00:00:01, 3-Dienstag: 17:30:14-17:30:19-00:00:04-00:00:00, 30-Montag: 08:25:50-16:28:57-08:03:06-00:00:00, 5-Donnerstag: 08:17:11-22:07:50-03:45:09-10:05:28, 6-Freitag: 07:36:54-14:56:13-07:19:19-00:00:00, 9-Montag: 18:10:28-18:10:32-00:00:04-00:00:00}
it sorts all the ones's, then the two's and so on.
i would appreciate any good advice on what i am doing wrong, thanks in advance
CodePudding user response:
My theory that is that you are comparing string values when you are sorting and not the integer values. Try parsing the values from string to integer first, and then it should work.
sort((a, b) => int.parse(a.key.replaceAll(" ", "").split("-")[0]).compareTo(int.parse(b.key.replaceAll(" ", "").split("-")[0])))