猜我们有这样的枚举
enum Role{ MANAGER, ADMIN, USER }
而且我们还有课
public class User{
String userName;
Role userRole;
}
那么我们有一组用户。我想根据角色枚举对这个用户集进行排序。具有 MANAGER 角色的用户必须在前,具有 USER 角色的用户必须到集合的末尾。
假设您在名为yourList
. 当然,您可以直接添加到集合中,如果这更适合您的需求。
SortedSet<User> set = new TreeSet<User>(Comparator.comparing(User::userRole)
.thenComparing(User::userName));
set.addAll(yourList)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句