所以,我有一个嵌套的mutableList,我需要排序,但我不能让它排序。模型类如下:正如你所看到的,它有一个模型,里面有ChildrenItem和Children Item。
@Keep
data class ItemsItem( @field:SerializedName("children")
var children: MutableList<ChildrenItem?>? = null)
@Keep
data class ChildrenItem( @field:SerializedName("children")
var children: MutableList<ChildrenItem?>? = mutableListOf(),
)
字符串
对嵌套的mutableList进行排序的代码如下:
itemList.forEach { itemsItem ->
itemsItem?.children?.forEach { airportItem ->
airportItem?.children?.sortBy { it?.name }
}
}
型
在上面的代码中,当我下次访问itemList嵌套的两个对象时,这里的子对象不按名称排序。我也尝试了CASE_INSENSITIVE方法,但它不起作用。
1条答案
按热度按时间ijxebb2r1#
它按预期工作:https://pl.kotl.in/D2U_9OP7f
字符串
测试结果:
在[ItemsItem(children=[ChildrenItem(children=[],name=first),ChildrenItem(children =[],name=second),ChildrenItem(children=[],name=third),ChildrenItem(children=[],name =four)],name= childrenItem)]之前
在[ItemsItem(children=[ChildrenItem(children=[],name=first),ChildrenItem(children =[],name=four),ChildrenItem(children=[],name=second),ChildrenItem(children=[],name=third)],name= childrenItem)]之后