Java基于Enum常量的排序
我们有一个枚举
enum listE {
LE1,
LE4,
LE2,
LE3
}
此外,我们还有一个包含字符串["LE1","LE2","LE3","LE4"]
的列表。有没有一种方法可以根据枚举定义的顺序(而不是自然的 String
顺序)对列表进行排序。
排序后的列表应为["LE1", "LE4", "LE2", "LE3"]
。
We have an enum
enum listE {
LE1,
LE4,
LE2,
LE3
}
Furthermore, we have a list that contains the strings ["LE1","LE2","LE3","LE4"]
. Is there a way to sort the list based on the enum defined order (not the natural String
order).
The sorted list should be ["LE1", "LE4", "LE2", "LE3"]
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Enum
通过枚举的自然顺序(声明值的顺序)实现Comparable
。如果您只是通过解析创建枚举值(而不是字符串)列表,然后使用 Collections.sort 对该列表进行排序,它应该按照您想要的方式排序。如果您再次需要字符串列表,只需在每个元素上调用name()
即可转换回来。Enum<E>
implementsComparable<E>
via the natural order of the enum (the order in which the values are declared). If you just create a list of the enum values (instead of strings) via parsing, then sort that list usingCollections.sort
, it should sort the way you want. If you need a list of strings again, you can just convert back by callingname()
on each element.每个枚举常量都有一个 ordinal 值对应于其在枚举声明中的位置。您可以使用相应枚举常量的序数值为字符串编写比较器。
Every enum constant has an ordinal value corresponding to its position in the enum declaration. You can write a comparator for your strings using the ordinal value of the corresponding enum constant.
values()
方法按照定义的顺序返回。输出
values()
method returns in the order in which it is defined.Output
我使用以下方法按升序对
List
进行排序,它对我来说效果很好。I used following to sort my
List<theEnum>
in an ascending order, and it worked fine for me.如果您想要在
Enum
类中提供不同的排序顺序并且无法修改它,只需将int
分配给您的枚举字段并比较它:然后使用
If you want different sort order then provided in
Enum
class and you cannot modify it, just assignint
to your enum fields and compare it:and then use
根据规范,乔恩的答案是正确的:
不过,我想留下一个 Java8 示例,以防有人想要将字符串值映射到枚举并按枚举顺序排序。这样,您就可以将字符串映射到枚举,使用默认的可比值进行排序,然后使用
toString
将其映射回来。这会给你留下这样的结果:结果将是:
Jon's answer is correct per the specification:
However, I wanted to leave a Java8 example in case somebody wants to map string values to an enum and sort by the enum order. With that you can map your strings to the enum, sort using the default comparable, and then map it back using a
toString
. This leaves you with something like this:The result would be:
您可能应该查看枚举的 ordinal() 方法,它返回枚举类型在枚举类中出现的位置的整数,因此在您的情况下 LE1 = 0,LE4 = 1 等...
you should probably look at the ordinal() method of the enum, it returns an Integer of the position the enum type appears in the enum class, so in your case LE1 = 0, LE4 = 1, etc...
尝试使用:
添加到枚举字段(排序字段),
例如
使用
TreeSet
使用
MyEnum.sorted
实现Comparator
归档Try to use :
add to enum field(sorted field)
like
Use
TreeSet
Implement
Comparator
usingMyEnum.sorted
filed您可以将以下方法添加到枚举中,以获取已排序的枚举常量数组:
其中 Element 是枚举的名称
枚举的排序位置通过他们的 toString
Here is a method you can add to your enumeration to get an array of sorted enumerated constants:
Where Element is the name of your Enumeration
Where the enumerations are sorted by their toString
如果您想按序数排序,可以使用
valueOf
转换字符串并将它们添加到 EnumSet(按序数排序),否则您可以根据枚举的属性对值进行排序。 (这可以更稳健,并且不依赖于声明枚举的顺序)使用 valueOf 并编写自定义比较器。
If you wan to sort by ordinal you can use
valueOf
to convert the string and add these to an EnumSet (which is sorted by ordinal)Otherwise you can sort the values based on an attribute of the enum. (This can be more robust and not dependent of the order the enums are declared) Use valueOf and write a custom Comparator.