英文:
How to sort MutableList<File> using Collator?
问题
我想要使用<code>listFiles()</code>方法对输出进行排序,使用<code>Collator.getInstance(Locale.ENGLISH)</code>方法。我应该如何操作?
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("EXTERNAL_STORAGE"))
val filefilter = FileFilter { file -> file.isDirectory && !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<File>
with a Comparator<String>
, that's why. There's no type warning because Collator
is actually a Comparator<Any>
, 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 -> collator.compare(a.name, b.name) }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论