Java 字符串数字比较器
我有一个方法返回需要排序的字符串列表。然而,我遇到了旧的字符串数字排序问题,想知道是否有人可以帮助实现比较器或为我指明一个方向。
该列表将返回以下列表:
State Lower Legislative District 1
State Lower Legislative District 11
State Lower Legislative District 12
...
State Lower Legislative District 2
...
State Lower Legislative District 100
...
State Upper Legislative District 1
State Upper Legislative District 11
...
因此,首先我需要进行基本的字符串排序,然后我需要按数字排序。排序所依据的数字应始终尾随,并且可以是 2 或 3 位数字。
(编辑)我最初的想法是在空间上分割字符串,在数字部分运行 StringUtils.isNumeric,然后排序。然而,这对我来说似乎有点混乱。
有人可以帮忙吗?
I have a method returning a list of String that need to be sorted. However, I'm running into the old String number sorting issue and was wondering if any one could assist with a Comparator implementation or point me in the direction of one.
The list is going to return something list this:
State Lower Legislative District 1
State Lower Legislative District 11
State Lower Legislative District 12
...
State Lower Legislative District 2
...
State Lower Legislative District 100
...
State Upper Legislative District 1
State Upper Legislative District 11
...
So, first I need to do a basic String sort, but then I need to sort by the number. The number to sort on should always trail, and may be 2 or 3 digits.
(Edit) My initial thought is to split the string on space, run StringUtils.isNumeric on the number portion, then sort. However, it seems a bit of a kludge to me.
Can anyone assist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Coding Horror 上有一篇关于此的文章。这称为自然排序,您可以在其中有效地处理一组数字作为单个“字符”。有关一些 Java 的信息,请参阅此问题该想法的实施。
There is an article about this on Coding Horror. This is called natural sorting, where you effectively treat a group of digits as a single "character". See this question for some Java implementations of the idea.
我编写了 String.CompareTo 的变体,用于比较两个字符串中找到的数字的长度。当遇到两个相同长度的数字时,字母数字比较将恢复正常。它还会跳过前导零。
I wrote a variation on String.CompareTo that compares the length of numbers found in the two strings. When encounting two numbers of the same length the alphanumeric compare is resumed as normal. It also skips leading zeros.
一种方法是使用简单的正则表达式来解析比较器中感兴趣的字段,然后手动比较它们。这是一个未经测试的示例:
One way would be to use a simple regex to parse out the fields of interest in your comparator and then compare them manually. Here's an untested example:
你有两个选择。第一个是创建一个具有两个字段的类 - 名称和号码。当然首先解析名称和号码。然后在比较器中先比较名称,然后比较数字。第二个是在
compare
方法中进行解析。选择哪一个更适合您。You have two options. The first one is to create a class having two fields - the name and the number. Of course first parse the name and numbers. Then in the comparator first compare the name and then the number. The second one is to do the parsing at place in the
compare
method. Choose which one is more appropriate to you.看看这个实现:
它应该很快,没有任何正则表达式或数组操作,只有几个标志和很多情况。
这应该对字符串内的数字的任意组合进行排序,并正确支持相等并继续前进的数字。
Have a look at this implementation:
It should be fast, without any regular expressions or array manipulation, just a couple of flags and a lot of cases.
This should sort any combination of numbers inside strings and properly support numbers which are equal and move on.
我通常通过在数字前添加零来实现此目的,并将整个实体作为字符串处理。然后排序。
看看这个:
I usually do this by prefixing zeros to the number and handle the whole entity as a string. then sort it.
See this:
一个简单的实现如下所示(这适用于任何以数字结尾的字符串):
A simple implementation would be like this one (this works with any string that ends with a number):