Java是否有一个函数按字典顺序排列列表(而不是它们的元素)? [英] Does Java have a function that lexicographically orders lists (not their elements)?
问题描述
Ord a => [a]
有一个词典排序。例如, >比较[1,2,3] [2,3,4]
LT
> compare [1] [1,2]
LT
这是大多数语言都有字符串排序。例如,在Java中,
abc.compareTo(bcd)// -1
a .compareTo(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]
LT
This is an immediate generalization of the lexicographical ordering most languages have on strings. In Java, for example,
"abc".compareTo("bcd") // -1
"a".compareTo("ab") // -1
Is 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.
这篇关于Java是否有一个函数按字典顺序排列列表(而不是它们的元素)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!