Java中如何根据对象的字符串值进行排序
我有 Java bean 类,我想按 String 类型的一个 bean 属性对这些 bean 列表进行排序。我怎样才能做到这一点?
I have Java bean class and I want to sort list of these beans by one bean attribute that is of String type. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要么使类型本身实现
Comparable;
,通过比较两个字符串来实现compareTo
方法,或者实现一个比较器
再次通过字符串进行比较。使用第一种方法,您就可以直接使用
Collections.sort()
;第二个你会使用Collections.sort(collection, new FooComparator());例如:
Either make the type itself implement
Comparable<Foo>
, implementing thecompareTo
method by comparing the two strings, or implement aComparator<Foo>
which again compares by the strings.With the first approach, you'd then just be able to use
Collections.sort()
directly; with the second you'd useCollections.sort(collection, new FooComparator());
For example:
通过使用自定义比较器?
By using a custom Comparator?
使用Guava,它只是
Using Guava, it's just