kotlin - 使用 Kotlin 按字母对数组进行排序

标签 kotlin

我正在为我的手机制作一个启动器,我需要按字母对应用程序进行排序。

 Appslist = ArrayList<AppInfo>()

    val i = Intent(Intent.ACTION_MAIN, null)
    i.addCategory(Intent.CATEGORY_LAUNCHER)
    val allApps = this.packageManager.queryIntentActivities(i, 0)

    for (ri in allApps) {
        val app = AppInfo()
        app.label = ri.loadLabel(this.packageManager)
        app.packageName = ri.activityInfo.packageName
        app.icon = ri.activityInfo.loadIcon(this.packageManager)
        if(app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord.toUpperCase() && searchWord != "" ||
            app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord.toLowerCase() && searchWord != "" ||
            app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord.capitalize() && searchWord != "" ||
            app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord && searchWord != ""){

            if(app.packageName != "com.david.launcher" ){
                Appslist.add(app)
            }

        }
        if(searchWord == ""){
            if(app.packageName != "com.david.launcher"){
                Appslist.add(app)
            }
        }

    }

这是我的列表类型(我不知道它是否称为列表类型,但我希望你能理解):
public class AppInfo {
internal var label: CharSequence? = null
internal var packageName: CharSequence? = null
internal var icon: Drawable? = null
internal var isInFav: Boolean? = false

最佳答案

惯用的方法是使用 sortedBy List 的扩展方法如果要排序到列表的副本中。或使用 sortBy MutableList 的扩展名如果您想在没有副本的情况下就地排序。一个 ArrayList可以作为任一列表类型使用。

// Sort a readonly list into a copy of the list

val appsList: List<AppInfo> = ...

val sortedAppsList = appsList.sortedBy { it.label?.toString() }
相对:
// Sort a mutable list in-place

val appsList: MutableList<AppInfo> = ...

appList.sortBy { it.label?.toString() }
如果持有 ArrayList直接引用这个具体类型是相同的,但不是惯用的。
// Sort an ArrayList list into a copy of the list

val appsList: ArrayList<AppInfo> = ...  // ALERT! not idiomatic

val sortedAppsList = appsList.sortedBy { it.label?.toString() }

// or if you want, feel free to sort in-place

appsList.sortBy { it.label?.toString() }
注意 toString()label: CharSequence成员。您必须小心对 CharSequence 类型的引用进行排序因为它是未定义的,它的排序行为是什么(参见:https://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html)

This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence is therefore, in general, undefined.


如果 CharSequence已经是 String (可能是),那么调用 toString() 并没有什么坏处因为它只是返回自己。
还要记住,可以为空的 CharSequence也必须处理,并且您需要决定在哪里需要空值:在列表的开头或结尾。我认为默认设置是让他们从头开始。

有关问题中代码的其他说明:
使用 ListMutableList接口(interface)而不是具体类来引用类型,并使用 Kotlin stdlib 中的方法对列表执行操作。也可以使用 val而不是 var对于不会改变的引用(意味着它总是指向同一个列表,不管列表内容是否会改变)。
if声明你可以减少很多,从...
if(app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord.toUpperCase() && searchWord != "" ||
    app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord.toLowerCase() && searchWord != "" ||
    app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord.capitalize() && searchWord != "" ||
    app.label?.toString()!!.length >= searchWord.length && app.label?.toString()!!.substring(0, searchWord.length) == searchWord && searchWord != ""){

    if(app.packageName != "com.david.launcher" ){
        Appslist.add(app)
    }

}
if(searchWord == ""){
    if(app.packageName != "com.david.launcher"){
        Appslist.add(app)
    }
}
更简单:
if (app.packageName != "com.david.launcher" &&
        (searchWord.isBlank() || 
         app.label?.startsWith(searchWord, ignoreCase = true) == true)) {
    appsList.add(app)
}
你应该 browse the standard library了解可用的内容,以便为 future 扩展工具包。

关于kotlin - 使用 Kotlin 按字母对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53351465/

相关文章:

kotlin - TeamCity Kotlin DSL 中元运行器的替代品是什么?

spring - Kotlin 与 Spring DI : lateinit property has not been initialized

java - 无法在android java中实现自定义日历 View 库

android - mockkStatic 和 mockkObject 不会模拟 Android 中的伴随对象

java - 如何在 kotlin 中模拟相同的行为

java - Kotlin:无法从对象按需导入

kotlin - Dispatcher IO 和 Default 用法的区别

android - 在 Webview 上处理 HTML onClick 函数。 shouldOverrideUrlLoading() 未调用

kotlin - 选择组合框中的项目时调用函数 TornadoFX

kotlin - 在不使用正则表达式的情况下有效地将 Kotlin 字符串分解为固定长度的子字符串