寻找字母顺序和自然顺序的组合(又名用户理智排序) [英] Looking for a combination of alphabetical and natural order (aka. user sane sorting)

查看:156
本文介绍了寻找字母顺序和自然顺序的组合(又名用户理智排序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很容易找到premade,但似乎我在网上找到的任何解决方案只能解决部分问题。

I thought this would be easy to find premade, but it seems any solution I can find on the web solves only part of the problem.

我想要排序用户提供的文件名列表(文件大多以人和/或地址命名),有时用不同的语言(大多数是德语,有点法语和意大利语混合)在这里和那里,很少有任何其他西方语言。)

I want to sort a list of Filenames (and the files have mostly been named after persons and/or addresses) that have been given by users, sometimes in different languages (mostly german, with a bit of french and italian mixed in here and there, and rarely any other western language).

这个想法是以(德国)用户普遍认为合理的方式呈现这个列表。这意味着订单应该遵循 java.text.Collat​​or for Locale.GERMAN ,但同时期望是对字符串中的数字进行例外处理,因此10出现在2之后。

The idea is to present this list ordered in a way that the (german) users generally deem sane. That means the order should follow the java.text.Collator for Locale.GERMAN, but at the same time the expectation is to make an exception for numbers in the string, so "10" comes after "2".

我找到了在网上进行自然排序的代码,但它依赖于逐字符比较(而Collat​​or不支持)。我可以使用子字符串来破解某些内容,但在比较器内部,我不认为在每次比较调用时创建多个子字符串是最明智的想法。

I have found code to do the natural sorting on the web, but it relies on comparing character by character (and Collator doesn't support that). I could hack something with substring, but inside a comparator it doesn't deem me the brightest idea to create multiple substrings on each compare-call.

任何想法如何实现这有效(在执行时间和实现时间),还是更好的测试和即用型实现?

Any ideas how to implement this efficiently (in both execution time and implementation time), or better yet a tested and ready-to-use implementation?

推荐答案

如果您使用@millimoose(http://www.davekoelle.com/alphanum.html)建议的比较器,请修改它以通过Collat​​or

If you use the Comparator suggested by @millimoose (http://www.davekoelle.com/alphanum.html) modify it to pass the Collator

public class AlphanumComparator implements Comparator
{
private Collator collator;
public AlphanumComparator(Collator collator) {
    this.collator = collator;
}
.....
    public int compare(Object o1, Object o2)
    {
......
result = thisChunk.compareTo(thatChunk); //should become
collator.compare(thisChuck, thatChuck);
....

此代码似乎有问题,例如01然后是2。但这取决于您的偏好,如果这很重要,请将其修改为在数字比较之前跳过前导零。

this code seems to have a problem, for example "01" is grater then "2". But this depends on you preference, if this is important modify it to skip the leading zeros before number compare.

这篇关于寻找字母顺序和自然顺序的组合(又名用户理智排序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆