在可能包含数字的字符串上排序 [英] Sort on a string that may contain a number

查看:255
本文介绍了在可能包含数字的字符串上排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个比较字符串的Java Comparator类,但是有一个扭曲。如果两个字符串的比较是相同的,在字符串的开头和结尾是相同的,不同的中间部分是一个整数,然后根据这些整数的数值进行比较。例如,我希望以下字符串结束,以便显示:




  • aaa

  • bbb 3 ccc

  • bbb 12 ccc

  • ccc 11

  • ddd

  • eee 3 ddd jpeg2000 eee

  • eee 12 ddd jpeg2000 eee



你可以看到,在字符串中可能有其他整数,所以我不能只使用正则表达式打破任何整数。我想的只是从开始走的字符串,直到我找到一个不匹配的位,然后从结束走,直到我找到一个不匹配的位,然后比较中间的位到正则表达式[0-9] +,如果比较,则进行数值比较,否则进行词法比较。



有更好的方法吗? / p>

更新我不认为我可以保证字符串中的其他数字,可能匹配的数字,没有空格

解决方案

字母算法



从网站



人们用不同于软件的数字排序字符串,大多数排序算法比较ASCII值,产生与人类逻辑不一致的排序,这里是如何解决它。



编辑:此链接指向该网站的 Java比较器实现。 / p>

I need to write a Java Comparator class that compares Strings, however with one twist. If the two strings it is comparing are the same at the beginning and end of the string are the same, and the middle part that differs is an integer, then compare based on the numeric values of those integers. For example, I want the following strings to end up in order they're shown:

  • aaa
  • bbb 3 ccc
  • bbb 12 ccc
  • ccc 11
  • ddd
  • eee 3 ddd jpeg2000 eee
  • eee 12 ddd jpeg2000 eee

As you can see, there might be other integers in the string, so I can't just use regular expressions to break out any integer. I'm thinking of just walking the strings from the beginning until I find a bit that doesn't match, then walking in from the end until I find a bit that doesn't match, and then comparing the bit in the middle to the regular expression "[0-9]+", and if it compares, then doing a numeric comparison, otherwise doing a lexical comparison.

Is there a better way?

Update I don't think I can guarantee that the other numbers in the string, the ones that may match, don't have spaces around them, or that the ones that differ do have spaces.

解决方案

The Alphanum Algorithm

From the website

"People sort strings with numbers differently than software. Most sorting algorithms compare ASCII values, which produces an ordering that is inconsistent with human logic. Here's how to fix it."

Edit: Here's a link to the Java Comparator Implementation from that site.

这篇关于在可能包含数字的字符串上排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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