Java中的自然排序顺序字符串比较 - 是内置的吗? [英] Natural sort order string comparison in Java - is one built in?

查看:19
本文介绍了Java中的自然排序顺序字符串比较 - 是内置的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要某种保留自然排序顺序的字符串比较函数1.Java中是否有类似的东西?我在 String 类比较器类只知道两种实现.

I'd like some kind of string comparison function that preserves natural sort order1. Is there anything like this built into Java? I can't find anything in the String class, and the Comparator class only knows of two implementations.

我可以自己动手(这不是一个很难的问题),但如果不需要的话,我宁愿不重新发明轮子.

I can roll my own (it's not a very hard problem), but I'd rather not re-invent the wheel if I don't have to.

在我的特定情况下,我有要排序的软件版本字符串.所以我希望1.2.10.5"被认为大于1.2.9.1".

1 通过自然"排序顺序,我的意思是它以人类比较字符串的方式比较字符串,而不是只对程序员有意义的ascii-betical"排序顺序.换句话说,image9.jpg"小于image10.jpg",album1set2page9photo1.jpg"小于album1set2page10photo5.jpg",1.2.9.1"小于1.2.10.5"

1 By "natural" sort order, I mean it compares strings the way a human would compare them, as opposed to "ascii-betical" sort ordering that only makes sense to programmers. In other words, "image9.jpg" is less than "image10.jpg", and "album1set2page9photo1.jpg" is less than "album1set2page10photo5.jpg", and "1.2.9.1" is less than "1.2.10.5"

推荐答案

在 Java 中,自然"顺序的含义是字典式"顺序,因此在核心中没有您正在寻找的实现.

In java the "natural" order meaning is "lexicographical" order, so there is no implementation in the core like the one you're looking for.

>

有开源实现.

这是一个:

NaturalOrderComparator.java

请务必阅读:

Cougaar 开源许可

我希望这会有所帮助!

这篇关于Java中的自然排序顺序字符串比较 - 是内置的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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