Sorting objects

Method ①:

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])
* the element object to sort
*/
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();
    }
}

 Method ②:

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());
         }   
     });        
}

 Method ③:

import java.util. *;  
  
/* Sort the collection  
    The Collections.sort() method can be used in the List collection
    When you need to reverse the order, you can use the Collections.reverseOrder() method, which returns a reverse order comparator
*/  
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());//sort in reverse order  
        System.out.println(ls);  
        Collections.sort(ls,Collections.reverseOrder(new StrLenComp())); //Reverse a custom comparator  
        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); //sort in natural order  
        System.out.println(ls);  
        Collections.sort(ls,new StrLenComp()); //Customize a comparator  
        System.out.println(ls);  
    }  
}  

 Case:

// Log in to the background user
    @RequestMapping(value="adminLogin",method = RequestMethod.POST)
    public String adminLogin(PrintWriter printWriter,Model model,HttpServletRequest request,String userName,String pwd)
    {
        //Query user by username and password
        WxUser user=userService.findUserByNameAndPwd(userName,pwd);
        request.getSession().setAttribute("sessionUser",user);
        if(null!=user)
          {

              //You can log in, the conditions have been verified in the previous ajax, and forwarded to the home page of the background management
              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);//Add all elements of set to list
              Collections.sort(wxGongnengquanxianList, new Comparator<WxGongnengquanxian>() {//sort
                  @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 {
              // Jump to the login failure page
              logService.LogStorage(request,SystemConstant.LOG_ACTION_LOGIN, SystemConstant.LOG_ACTION_FAIL,"后台登陆");
          }
        return "/background/bglogin";

    }

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327029445&siteId=291194637