我有:
var schedulesByMonth = HashMap<String, MutableList<Schedule>>()
附表有startDate: Date
属性。
我从一个巨大的Schedule
对象列表开始,按月将它们排序到hashmap中。因此,如果Schedule对象上的startDate是2018年6月,它将进入密钥June 2018
的列表。
这一切都很好,但我需要按月顺序排序的选择键:
schedulesByMonth.keys
如果该数组为["July 2018", "August 2018", "June 2018"]
,我需要它为["June 2018", "July 2018", "August 2018"]
。
我知道我可以创建一个sortedMap:
val sorted = schedulesByMonth.toSortedMap(compareBy<String> {
}
但是只对键进行排序(String
)。如何将it
设为值MutableList<Schedule>
,以便我可以按其中一个时间表的startDate
进行排序?
答案 0 :(得分:1)
您可以使用sortedWith
对条目进行排序,并将比较器作为参数。可以使用compareBy
轻松创建比较器:
schedulesByMonth.entries.sortedWith(compareBy({ it.value[0].startDate })
答案 1 :(得分:0)
我认为这里的主要问题是错误的密钥类。只需用日期替换字符串,一切都会轻松得多。