【发布时间】:2020-05-11 20:13:21
【问题描述】:
我有这个改造响应体为 LinkedHashMap
"-M74DWOrW0w07BpfmBVo": {
"noteContent": "Note 1 content",
"noteCurrentTime": 1589225588206,
"noteTitle": "Note 1"
},
"-M74Dc2dDAZgVk6q86Rs": {
"noteContent": "Note 2 content",
"noteCurrentTime": 1589225990674,
"noteTitle": "Note 2"
},
"-M74DmbSNQnjEU0Hw4yQ": {
"noteContent": "Note 3 content",
"noteCurrentTime": 1589225658614,
"noteTitle": "Note 3"
}
}
我需要按“noteCurrentTime”值排序。到目前为止,这就是获取排序值数组的方法。
private fun sortJsonArray(valuesArray: JSONArray): JSONArray? {
val sortedValues: MutableList<JSONObject> = ArrayList()
for (i in 0 until valuesArray.length()) {
sortedValues.add(valuesArray.getJSONObject(i))
}
sortedValues.sortWith(Comparator { lhs, rhs ->
val lid: Long = lhs.getLong("noteCurrentTime")
val rid: Long = rhs.getLong("noteCurrentTime")
lid.compareTo(rid)
})
return JSONArray(sortedValues)
}
但是,这只会返回排序后的值,没有键,它们现在的顺序是错误的。有没有办法对 LinkedHashMap 的值进行排序并保持键的正确顺序?任何和所有的帮助将不胜感激。
【问题讨论】:
标签: java android kotlin retrofit linkedhashmap