【问题标题】:How to sortBy and groupBy with the same List in kotlin如何在kotlin中使用相同的List对sortBy和groupBy进行排序
【发布时间】:2020-04-19 21:42:15
【问题描述】:

我有一个列表,其中包含具有标题的元素,我使用 groupBy 将所有列表元素按一个标题分组,这很好用,但是,现在我还想对 hasDisscount 为 true 的元素进行排序,因为我希望这些元素首先显示在列表中

 fun setItems(listProduct: MutableList<Product>) {
        listProduct.sortBy { it.hasDisscount }
        listProduct.groupBy({ it.productTypeName }, { product -> product
        }).iterator().forEach {
            uiItems.add(it.key)
            for (product in it.value) {
                uiItems.add(product)
            }
        }
        notifyDataSetChanged()
    }

现在,我尝试在分组之前先使用 hasDisscount 为 true 的元素对列表进行排序,但不起作用,我的折扣元素显示在列表的底部

有什么建议吗?

【问题讨论】:

    标签: android list kotlin mutablelist


    【解决方案1】:

    通过添加修复它

       listProduct.sortByDescending { it.hasDisscount }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2021-08-27
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多