方法①:
package collsort.comparable; /** * Created by IntelliJ IDEA. * User: leizhimin * Date: 2008-3-29 22:21:19 * Company: LavaSoft([url]http://lavasoft.blog.51cto.com[/url]) * 要排序的元素对象 */ public class Cat implements Comparable<Cat> { private int age; private String name; public Cat(int age, String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "Cat{" + "age=" + age + ", name='" + name + '\'' + '}'; } public int compareTo(Cat o) { return this.getAge() - o.getAge(); } }
方法②:
public static void hashMapSortTest() { Map<String, Integer> maps = new HashMap<String, Integer>(); maps.put("boy", 8); maps.put("cat", 7); maps.put("dog", 1); maps.put("apple", 5); Iterator i = maps.entrySet().iterator(); while (i.hasNext()) { Map.Entry<String, Integer> entry1 = (Map.Entry<String, Integer>) i.next(); } List<Map.Entry<String, Integer>> info = new ArrayList<Map.Entry<String, Integer>>(maps.entrySet()); Collections.sort(info, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2) { return obj1.getValue().compareTo(obj2.getValue()); } }); }
方法③:
import java.util.*; /*集合排序 List集合中可以使用Collections.sort()方法 在需要求逆序时可以使用Collections.reverseOrder()方法,该方法返回一个逆序比较器 */ class StrLenComp implements Comparator<String> { public int compare(String o1,String o2) { return o1.length()==o2.length()? o1.compareTo(o2):o1.length()-o2.length(); } } class CollectionDemo { public static void main(String[] args) { SortDemo(); ReverseDemo(); } public static void ReverseDemo() { List<String> ls = new ArrayList<String>(); ls.add("yo"); ls.add("a"); ls.add("you"); ls.add("ada"); System.out.println(ls); Collections.sort(ls,Collections.reverseOrder());//逆序排序 System.out.println(ls); Collections.sort(ls,Collections.reverseOrder(new StrLenComp())); //逆序一个自定义比较器 System.out.println(ls); } public static void SortDemo() { List<String> ls = new ArrayList<String>(); ls.add("you"); ls.add("a"); ls.add("you"); ls.add("ada"); System.out.println(ls); Collections.sort(ls); //按照自然顺序排序 System.out.println(ls); Collections.sort(ls,new StrLenComp()); //自定义一个比较器 System.out.println(ls); } }
案例:
//对后台用户用进行登录操作 @RequestMapping(value="adminLogin",method = RequestMethod.POST) public String adminLogin(PrintWriter printWriter,Model model,HttpServletRequest request,String userName,String pwd) { //通过用户名和密码查询用户 WxUser user=userService.findUserByNameAndPwd(userName,pwd); request.getSession().setAttribute("sessionUser",user); if(null!=user) { //可以进行登录,条件在前面的ajax已经校验过了,转发到后台管理的首页 Set<WxRole> wxRoleSet=user.getWxRoles(); Set<WxGongnengquanxian> gongnengquanxians=new HashSet<WxGongnengquanxian>(); for(WxRole wxRole:wxRoleSet){ gongnengquanxians= wxRole.getWxGongnengquanxians(); } List<WxGongnengquanxian> wxGongnengquanxianList = new ArrayList<WxGongnengquanxian>(); wxGongnengquanxianList.addAll(gongnengquanxians);//将set所有元素添加到list Collections.sort(wxGongnengquanxianList, new Comparator<WxGongnengquanxian>() {//进行排序 @Override public int compare(WxGongnengquanxian o1, WxGongnengquanxian o2) { return o2.getWxGongnengquanxianName().compareTo(o1.getWxGongnengquanxianName()); } }); model.addAttribute("gongnengquanxians",wxGongnengquanxianList); request.getSession().setAttribute("gnqx",gongnengquanxians); logService.LogStorage(request,SystemConstant.LOG_ACTION_LOGIN, SystemConstant.LOG_ACTION_SUCCESS,"后台登陆"); return "/background/bgIndex"; }else { //跳转到登录失败页面 logService.LogStorage(request,SystemConstant.LOG_ACTION_LOGIN, SystemConstant.LOG_ACTION_FAIL,"后台登陆"); } return "/background/bglogin"; }