简体   繁体   English

排序 arraylist 并返回 arraylist(不是列表) - Kotlin

[英]Sorting arraylist and returning an arraylist (not a list) - Kotlin

I'm trying to display sorted information using a RecyclerView .我正在尝试使用RecyclerView显示排序信息。 To do so, I've got an ArrayList of custom datatype.为此,我有一个自定义数据类型的ArrayList I want to sort the data in the ArrayList by descending order, using one of the fields in the ArrayList .我想使用ArrayList中的字段之一按降序对ArrayList中的数据进行排序。 I'm familiar with the methods that are already available ( sortWith , sortBy , sortByDescending , etc) but unfortunately, they all change from ArrayList to List and this stops the recycler view from working.我熟悉已经可用的方法( sortWithsortBysortByDescending等),但不幸的是,它们都从ArrayList更改为List ,这会使回收站视图停止工作。 Ideally, I'd like to preserve the ArrayList because I need it for other processes that I carry out later in the project.理想情况下,我想保留ArrayList ,因为我需要它用于我稍后在项目中执行的其他流程。

WHAT I'M TRYING TO ACHIEVE:我想要达到的目标:

INPUT: arrayListName = ArrayList<CustomType>输入: arrayListName = ArrayList<CustomType>

PROCESS: [sort by decending order of arrayListName.firstName ]过程:[按arrayListName.firstName的降序排序]

OUTPUT: return ArrayList<CustomType> OUTPUT: return ArrayList<CustomType>

I've tried casting the List (shown below) to ArrayList , but that throws the below error:我尝试将List (如下所示)转换为ArrayList ,但这会引发以下错误:

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList 

Line of code that throws the error (I've tried various methods, but they all throw an exception on that line):抛出错误的代码行(我尝试了各种方法,但它们都在该行抛出异常):

val sortedModel = model.sortedWith(compareBy { it.name}) as ArrayList<Model>

Any help is appreciated(:任何帮助表示赞赏(:

I'm happy with a java solution too because I understand it and can easily implement from java to kotlin.我对 java 解决方案也很满意,因为我理解它并且可以轻松地从 java 到 kotlin 实施。

You can not cast a List to an ArrayList directly.您不能将List直接转换为ArrayList If you want to convert a List to ArrayList , you can do it this way:如果要将List转换为ArrayList ,可以这样做:

val sortedModel = model.sortedByDescending { it.name }.toCollection(ArrayList())

This will help you这将帮助你

val list = arrayList.sortedByDescending { it.firstName.length }
        result.addAll(list)

Full code:完整代码:

val arrayList: ArrayList<CustomType> = arrayListOf()
val result: ArrayList<CustomType> = arrayListOf()
arrayList.add(CustomType("Aadddd"))
arrayList.add(CustomType("Ccd"))
arrayList.add(CustomType("Bbdd"))
arrayList.add(CustomType("Ffddd"))
arrayList.add(CustomType("Dd"))

val list = arrayList.sortedByDescending { it.firstName.length }
result.addAll(list)

Output: Output:

SORT [{"firstName":"Aadddd"},{"firstName":"Ffddd"},{"firstName":"Bbdd"},{"firstName":"Ccd"},{"firstName":"Dd"}]

Use sortWith instead it does inplace, try this使用sortWith代替它就地,试试这个

data class A(
    val x : Int
)

fun main(args: Array<String>) {
    var a = arrayListOf( A(1) ,A(2) ,A(3) ,A(4) )
    a.sortWith(compareByDescending { it.x })
    assert( a is ArrayList )
    println(a)
}

try yourself自己试试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM