https://kotlinlang.org logo
Title
s

Stefan Oltmann

08/02/2021, 8:33 AM
I wonder if the the Kotlin stdlib comes with an alphanumeric comparator for "natural order" of file names like https://github.com/sawano/alphanumeric-comparator
1
This is a shorter example in Kotlin: https://rosettacode.org/wiki/Natural_sorting#Kotlin
Since there are is so many functionality of Guava and Apache Commons build into the kotlin stdlib I somehow refuse to believe this alpha numeric sort should not be part of it... Please tell me that I'm just blind.
e

ephemient

08/02/2021, 8:44 AM
stdlib doesn't contain any locale stuff and natural sorting is pretty tied up in that
s

Stefan Oltmann

08/02/2021, 8:44 AM
Where would I look for that?
e

ephemient

08/02/2021, 8:59 AM
not sure what you mean
depends on what platform you're on; for example, if you have ICU4J you can use
(Collator.getInstance() as RuleBasedCollator).apply { numericCollation = true }
for natural sorting you should use a locale-aware collator anyway, which is simply not cross-platform/stdlib
s

Stefan Oltmann

08/02/2021, 9:01 AM
Ok, I see. It's up to third party frameworks then. Maybe a small chance that Kotlin/Native would get it as an extension.
It may not be cross-plattform, yes, but in Windows Explorer and macOS Finder I see the same natural sorting I would expect anywhere. So it would have made sense to me that there is a place for that in a Kotlin lib
👎🏼 1
e

ephemient

08/02/2021, 9:03 AM
I would not expect Kotlin to ship its own copy of a library as huge as ICU, you should link it yourself if you need it
s

Stefan Oltmann

08/02/2021, 9:04 AM
Not sure about that. Kotlin has many features previously found only in Guava & Apache Commons
e

ephemient

08/02/2021, 9:05 AM
this is not a feature found in either Guava or Apache Commons as far as I know
to implement it properly you need a CLDR or an equivalent database of per-locale rules, which needs to be updated with Unicode changes
Apache Commons Collections contains it
And Kotlin Collections compared to JDK standard has really a lot of it already contained
e

ephemient

08/02/2021, 9:06 AM
naturalComparator is not "natural string order" you're talking about, it is the no-op "natural object order" comparator
s

Stefan Oltmann

08/02/2021, 9:07 AM
Oh, ok, I confused that
e

ephemient

08/02/2021, 9:08 AM
for comparison, Swift does require ICU, which is a pretty annoying dependency for non-macos users
and on Linux it bundles the CLDR database, which then ends up out of sync with what's on the system… this is not a simple problem
s

Stefan Oltmann

08/02/2021, 9:11 AM
Too bad 😕
So all these samples like https://rosettacode.org/wiki/Natural_sorting#Kotlin are defect because they miss out things?
e

ephemient

08/02/2021, 9:15 AM
that example there is assuming a english-like locale
s

Stefan Oltmann

08/02/2021, 9:19 AM
Ok, I see
Thanks.