这是大多数语言都有字符串排序。例如,在Java中,
abcpareTo(bcd)// -1 a pareTo(ab)// -1标准库中是否有Java函数或者其他实现Haskell列表的字典顺序的库?我希望它有一个类型签名,符合
< T扩展Comparable< T>> int compare(List< T>,List< T>)自己实现并不困难,但我宁愿不重新发明轮子。 解决方案
如果第三方库是公平的游戏,那么在 Guava 这只是 Ordering.natural()。lexicographical() 。尽管如此,基本的Java并没有任何内容。
In Haskell, lists of type Ord a => [a] have a lexicographical ordering. For example,
> compare [1, 2, 3] [2, 3, 4] LT > compare [1] [1, 2] LTThis is an immediate generalization of the lexicographical ordering most languages have on strings. In Java, for example,
"abc"pareTo("bcd") // -1 "a"pareTo("ab") // -1Is there any Java function in the standard library or other library that implements the lexicographical ordering of lists that Haskell has? I would expect it have a type signature along the lines of
<T extends Comparable<T>> int compare(List<T>, List<T>)Implementing it myself wouldn't be difficult, but I'd rather not reinvent the wheel.
解决方案If third-party libraries are fair game, then in Guava this is just Ordering.natural().lexicographical(). There isn't anything for this built into basic Java, though.