Android 如何在Kotlin中多次筛选数据类

Android 如何在Kotlin中多次筛选数据类,android,list,kotlin,filter,collections,Android,List,Kotlin,Filter,Collections,我想编写对我的数据类/对象列表执行筛选的函数。我可以过滤这些类,但似乎每次我想再次过滤时都需要创建一个新的列表,有什么办法吗 我创建了多个函数和扩展函数,但它不起作用 我的数据类和自定义列表 data class Product( var name: String, var category: Category, var price: Double, var rating: Double ) object ProductList { var produc

我想编写对我的数据类/对象列表执行筛选的函数。我可以过滤这些类,但似乎每次我想再次过滤时都需要创建一个新的列表,有什么办法吗

我创建了多个函数和扩展函数,但它不起作用

我的数据类和自定义列表

data class Product(
    var name: String,
    var category: Category,
    var price: Double,
    var rating: Double
)

object ProductList {
    var productList = listOf(
        Product("Shopping Bag", Category.HOME, 11.75, 3.9),
        Product("Gold Earrings", Category.JEWELRY, 38.99, 4.2),
        Product("Golf Clubs", Category.SPORTS, 20.75, 4.1),
        Product("iPad", Category.ELECTRONICS, 180.75, 3.9),
        Product("MacBook Pro", Category.ELECTRONICS, 1200.85, 4.6),
        Product("Basketball Net", Category.SPORTS, 8.75, 3.5),
        Product("Lipstick", Category.WOMENS, 19.75, 4.1),
        Product("Dumbells", Category.HOME, 12.99, 4.8),
        Product("Gym Shoes", Category.MENS, 69.89, 3.9),
        Product("Coffee Mug", Category.HOME, 6.75, 3.9),
        Product("Reading Glasses", Category.MENS, 14.99, 2.8),
        Product("Nail Polish", Category.WOMENS, 8.50, 3.4),
        Product("Football Cleats", Category.SPORTS, 58.99, 3.9)
    )
}
我的两个过滤函数

fun filterByCategory(category: Category): List<Product> {
    return productList.filter { it.category == category }
}

fun filterByRating(productList: List<Product>,rating: Double): List<Product> {
    return productList.filter { it.rating >= rating }
}
fun sortByPriceLowToHigh(productList: List<Product>) {
    val sortedByPrice = productList.sortedBy { it.price  }
    for (i in sortedByPrice) {
        println("${i.name}: ${i.price}")
    }

}


fun sortByPriceHighToLow(productList: List<Product>) {
    val sortedByPrice = productList.sortedByDescending { it.price }
    for (i in sortedByPrice) {
        println("${i.name}: ${i.price}")
    }

}
我想要的输出是:

Sorted by Price Low to High
Dumbells: 12.99

Sorted by Price High to Low
Dumbells: 12.99
因为dumbbells是CATEGORY.HOME中评级高于4.0的唯一产品。 我知道我可以用一个新谓词在列表上调用filter两次,但我想使用function,这样我可以在多个位置进行这些调用。

如前所述:您应该使用这些函数的返回值,如
filter
sortedBy

Kotlin的collection API使用的是一种函数方法,您不需要修改传递给函数的值,而是返回结果

我建议您在这种情况下使用扩展函数。将它们链接起来看起来更好:

fun List<Product>.filterByCategory(category: Category) = this.filter { it.category == category }

fun List<Product>.filterByRating(rating: Double) = this.filter { it.rating >= rating }

fun List<Product>.sortByPriceLowToHigh() =
    this.sortedBy { it.price }
        .forEach { println("${it.name}: ${it.price}") }

fun List<Product>.sortByPriceHighToLow() =
    this.sortedByDescending { it.price }
        .forEach { println("${it.name}: ${it.price}") }

fun main() {
    val filteredProducts = ProductList.productList
        .filterByCategory(Category.HOME)
        .filterByRating(4.0)

    println("Sorted by Price Low to High")

    val lowToHighProducts = filteredProducts.sortByPriceLowToHigh()

    println("")
    println("Sorted by Price High to Low")

    val highToLowProducts = filteredProducts.sortByPriceHighToLow()
}
fun List.filterByCategory(category:category)=this.filter{it.category==category}
乐趣列表.filterByRating(评级:Double)=this.filter{it.rating>=rating}
有趣的列表。sortByPriceLowToHigh()=
这个.sortedBy{it.price}
.forEach{println(${it.name}:${it.price}”)}
有趣的列表=
this.sortedByDescending{it.price}
.forEach{println(${it.name}:${it.price}”)}
主要内容(){
val filteredProducts=ProductList.ProductList
.filterByCategory(类别.HOME)
.filterByRating(4.0)
println(“按价格从低到高排序”)
val lowtohightproducts=filteredProducts.sortByPriceLowToHigh()
println(“”)
println(“按价格高低排序”)
val highToLowProducts=filteredProducts.sortByPriceHighToLow()
}

您没有使用函数返回的值。请注意,筛选器不会修改原始列表,但会返回一个新的筛选列表。请检查我的答案,如果它对您有效,请接受它?
Sorted by Price Low to High
Dumbells: 12.99

Sorted by Price High to Low
Dumbells: 12.99
fun List<Product>.filterByCategory(category: Category) = this.filter { it.category == category }

fun List<Product>.filterByRating(rating: Double) = this.filter { it.rating >= rating }

fun List<Product>.sortByPriceLowToHigh() =
    this.sortedBy { it.price }
        .forEach { println("${it.name}: ${it.price}") }

fun List<Product>.sortByPriceHighToLow() =
    this.sortedByDescending { it.price }
        .forEach { println("${it.name}: ${it.price}") }

fun main() {
    val filteredProducts = ProductList.productList
        .filterByCategory(Category.HOME)
        .filterByRating(4.0)

    println("Sorted by Price Low to High")

    val lowToHighProducts = filteredProducts.sortByPriceLowToHigh()

    println("")
    println("Sorted by Price High to Low")

    val highToLowProducts = filteredProducts.sortByPriceHighToLow()
}