【问题标题】:Sort a list of Class by a specific attribute [duplicate]按特定属性对类列表进行排序[重复]
【发布时间】:2021-11-12 02:12:44
【问题描述】:

您好,我是 android 开发 (kotlin) 的新手,这是我的问题: 我有一个 ArrayList 类(ProductStore),在这个类中还有另一个类(StorageType),其中有一个字符串(libellus),我想用它对我的 ArrayList 进行排序(或者更确切地说,构成字符串(libellus)相同的组。我该怎么做?

data class Stock(
    val product: ArrayList<ProductStore>
)

class ProductStore {
    var quantity: Double? = null
    val expiration_date: String? = null
    val product: Product? = null
    val storage_type: StorageType? = null
    val quantity_type: QuantityType? = null
}

class Product{
    val id: Double? =  null
    val name: String? = null
    val image: String? =null
}

class StorageType{
    val id: Double? = null
    val libellus: String? = null
}

class QuantityType{
    val libellus: String? = null
}

【问题讨论】:

标签: android sorting kotlin


【解决方案1】:

问题标题很好地提示了您需要做什么;-) 只需使用 sortedBy() 功能:

val list: List<ProductStore> = ...

val sorted = list.sortedBy { it.storage_type?.libellus }

你说你使用ArrayList,所以如果你喜欢就地排序,那么你可以使用sortBy()

根据您需要在哪里放置null 项目(在顶部或底部),您可能需要修改此代码。

或者更确切地说构成字符串(libellus)相同的组

如果是这种情况,那么您可能根本不需要进行排序。您可以像这样对项目进行分组:

val grouped = list.groupBy { it.storage_type?.libellus }

它为您提供了一个映射,其中键是 libellus 字符串,值是与其关联的 ProductStore 对象的列表。

【讨论】:

  • 感谢您的回答,它对我有很大帮助,但是我知道“libelus”的价值(新鲜、环境或冷冻)我想根据这些价值创建组
  • 我不确定我是否理解正确。听起来 groupBy() 应该完全满足您的需求 - 它会为您提供:freshambient 等组。
【解决方案2】:

我尝试在 main 方法中重新创建您的代码。但首先我必须将 ProductStore()Product()StorageType()QuantityType() 普通 Kotlin 类转换为 Data Classes,以便使用隐式 toString() 轻松将它们打印为字符串。

然后我做了一个Stock Data Class的例子如下:

fun main() {
    
    //instance of the Stock Class
    val stock = Stock(
        product = arrayListOf(
            ProductStore(
                storage_type = StorageType(libellus = "frozen"),
                quantity = 13.0,
                expiration_date = "",
                product = Product(),
                quantity_type = QuantityType()
            ), ProductStore(
                storage_type = StorageType(libellus = "fresh"),
                quantity = 18.0,
                expiration_date = "",
                product = Product(),
                quantity_type = QuantityType()
            ), ......}}

从这里我只是使用@broot 方法将ArrayList of ProductStore 元素分组为Map 对象:

//grouping  the stock
    val productsGroups = stock.product.groupBy { productStore ->
        when (productStore.storage_type?.libellus) {

            "frozen" -> {
                "frozen"
            }
            "fresh" -> {
                "fresh"
            }
            else -> {
                "ambient"
            }

        }
    }
    println(productsGroups)
}

println(productsGroups) 的输出是打印的 Map Object,其中 Stock 的 StorageType(frozen、ambient、fresh)是键,对应的列表作为值。

{frozen=[ProductStore(quantity=13.0, expiration_date=, product=Product(id=null, name=null, image=null), storage_type=StorageType(id=null, libellus=frozen), quantity_type=QuantityType(libellus=null))], 

fresh=[ProductStore(quantity=18.0, expiration_date=, product=Product(id=null, name=null, image=null), storage_type=StorageType(id=null, libellus=fresh), quantity_type=QuantityType(libellus=null))], 

ambient=[ProductStore(quantity=2021.0, expiration_date=, product=Product(id=null, name=null, image=null), storage_type=StorageType(id=null, libellus=ambient), quantity_type=QuantityType(libellus=null))]}

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 2010-10-26
    • 2015-09-21
    • 1970-01-01
    • 2021-06-22
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    相关资源
    最近更新 更多