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

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

问题描述

我想某种形式的字符串比较函数preserves自然排序顺序 1 。有没有这样的内置的Java什么?我找不到在 String类的东西,而< A HREF =htt​​p://java.sun.com/javase/6/docs/api/java/util/Comparator.html>比较级只知道两个实现。

我可以推出自己的(这不是一个很难的问题),但我宁愿不重新发明轮子,如果我没有。

在我的特定情况下,我有我要排序的软件版本字符串。所以我想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

解决方案

在Java中的自然秩序意思是辞书命令,所以像你正在寻找一个核心没有实现。

有开源实现。

这里有一个:

NaturalOrderComparator.java

请务必阅读:

Cougaar的开源许可

我希望这有助于!

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.

In my specific case, I have software version strings that I want to sort. So I want "1.2.10.5" to be considered greater than "1.2.9.1".


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"

解决方案

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

There are open source implementations.

Here's one:

NaturalOrderComparator.java

Make sure you read the:

Cougaar Open Source License

I hope this helps!

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

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