在Java中自然排序顺序字符串比较 - 是一款内置?字符串、顺序、是一款、自然

2023-09-10 22:26:15 作者:〆絕卐戀ヤゞ

我想某种形式的字符串比较函数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中的自然秩序意思是辞书命令,所以像你正在寻找一个核心没有实现。

有开源实现。

这里有一个:

Java开发技术之字符串String类的特点

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!