如何使用Collator对MutableList\进行排序?

huangapple go评论92阅读模式
英文:

How to sort MutableList<File> using Collator?

问题

我想要使用&lt;code&gt;listFiles()&lt;/code&gt;方法对输出进行排序使用&lt;code&gt;Collator.getInstance(Locale.ENGLISH)&lt;/code&gt;方法我应该如何操作

    val path = File(Os.getenv("EXTERNAL_STORAGE"))
    val filefilter = FileFilter { file -> file.isDirectory && !file.isHidden }
    val fileList = path.listFiles(filefilter)!!.toList()
    Collections.sort(fileList, Collator.getInstance(Locale.ENGLISH))

我得到的错误是

    java.lang.ExceptionInInitializerError
        at com.praps.myapplication.ui.list.ListFragment.onCreateView(ListFragment.kt:43)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:221)
        at android.app.ActivityThread.main(ActivityThread.java:7520)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     引发此问题的原因是
     
     java.lang.ClassCastException: 无法将java.io.File强制转换为java.lang.String
        at java.text.Collator.compare(Collator.java:277)
        at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
        at java.util.TimSort.sort(TimSort.java:220)
        at java.util.Arrays.sort(Arrays.java:1492)
        at java.util.ArrayList.sort(ArrayList.java:1470)
        at java.util.Collections.sort(Collections.java:206)
        at com.praps.myapplication.ui.list.dummy.DummyContent
英文:

I want to sort the output from <code>listFiles()</code> using <code>Collator.getInstance(Locale.ENGLISH)</code>.
How do I do it?

    val path = File(Os.getenv(&quot;EXTERNAL_STORAGE&quot;))
    val filefilter = FileFilter { file -&gt; file.isDirectory &amp;&amp; !file.isHidden }
    val fileList = path.listFiles(filefilter)!!.toList()
    Collections.sort(fileList, Collator.getInstance(Locale.ENGLISH))

The error I get is

java.lang.ExceptionInInitializerError
    at com.praps.myapplication.ui.list.ListFragment.onCreateView(ListFragment.kt:43)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
    at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
    at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:221)
    at android.app.ActivityThread.main(ActivityThread.java:7520)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
 Caused by: java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
    at java.text.Collator.compare(Collator.java:277)
    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    at java.util.TimSort.sort(TimSort.java:220)
    at java.util.Arrays.sort(Arrays.java:1492)
    at java.util.ArrayList.sort(ArrayList.java:1470)
    at java.util.Collections.sort(Collections.java:206)
    at com.praps.myapplication.ui.list.dummy.DummyContent

答案1

得分: 1

你正在尝试使用Comparator<String>来对Array<File>进行排序,这就是原因。之所以没有类型警告,是因为Collator实际上是一个Comparator<Any>,但它的实现RuleBasedCollator不是,它只比较字符串。

你需要创建一个中间比较器,它接受文件的名称并进行比较:

val files = dir.listFiles().orEmpty()
val collator = Collator.getInstance(Locale.ENGLISH);
files.sortWith { a, b -> collator.compare(a.name, b.name) }
英文:

You're trying to sort a Array&lt;File&gt; with a Comparator&lt;String&gt;, that's why. There's no type warning because Collator is actually a Comparator&lt;Any&gt;, but its implementation RuleBasedCollator isn't, it only compares strings.

You need to make an intermediate comparator that takes the name of the files and compare that:

val files = dir.listFiles().orEmpty()
val collator = Collator.getInstance(Locale.ENGLISH);
files.sortWith { a, b -&gt; collator.compare(a.name, b.name) }

huangapple
  • 本文由 发表于 2020年10月1日 05:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64146025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定