zoukankan      html  css  js  c++  java
  • Java中如何实现类似C++结构体的二级排序

    1:实现Comparable接口

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    
    class User implements Comparable<User>{
    	private Integer id;
    	private String name;
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	@Override
    	public int compareTo(User o) {
    		if (this.id == o.id) return this.name.compareTo(o.name);  //id相同按name排序
    		else return this.id.compareTo(o.id); //按ID从小到大排序
    	}
    }
    
    public class Main {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		
    		String s1 = "abx";
    		String s2 = "小1";
    		System.out.println(s1.compareTo(s2));
    		
    		
    		List<User> users = new ArrayList<User>();
    		
    		User u6 = new User();
    		u6.setId(6); u6.setName("Jack");
    		
    		
    		User u = new User();
    		u.setId(6); u.setName("abx");
    		
    		
    		User u1 = new User();
    		u1.setId(1); u1.setName("小1");
    		
    		
    		User u4 = new User();
    		u4.setId(4); u4.setName("小4");
    		
    		
    		User u2 = new User();
    		u2.setId(2); u2.setName("小2");
    		
    		User u7 = new User();
    		u7.setId(6); u7.setName("abc");
    		
    		users.add(u2);
    		users.add(u6);
    		users.add(u4);
    		users.add(u1);
    		users.add(u);
    		users.add(u7);
    		Collections.sort(users);
    		
    		for (User ui : users) {
    			System.out.println(ui.getId() + " " + ui.getName());
    		}
    	}
    
    }
    

      

    2:集合外定义Comparator

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    
    class User{
    	private int id;
    	private String name;
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    
    public class Main {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		
    		String s1 = "abx";
    		String s2 = "小1";
    		System.out.println(s1.compareTo(s2));
    		
    		
    		List<User> users = new ArrayList<User>();
    		
    		User u6 = new User();
    		u6.setId(6); u6.setName("Jack");
    		
    		
    		User u = new User();
    		u.setId(6); u.setName("abx");
    		
    		
    		User u1 = new User();
    		u1.setId(1); u1.setName("小1");
    		
    		
    		User u4 = new User();
    		u4.setId(4); u4.setName("小4");
    		
    		
    		User u2 = new User();
    		u2.setId(2); u2.setName("小2");
    		
    		User u7 = new User();
    		u7.setId(6); u7.setName("abc");
    		
    		users.add(u2);
    		users.add(u6);
    		users.add(u4);
    		users.add(u1);
    		users.add(u);
    		users.add(u7);
    		
    		Collections.sort(users, new Comparator<User>() {
    			@Override
    			public int compare(User o1, User o2) {
    				if (o1.getId() == o2.getId()) return o1.getName().compareTo(o2.getName());
    				return o1.getId() > o2.getId() ? 1 : -1;
    			}
    			
    		});
    		
    		for (User ui : users) {
    			System.out.println(ui.getId() + " " + ui.getName());
    		}
    	}
    
    }
    

      

  • 相关阅读:
    迷宫救人——DFS小题
    spring boot配置service发布服务
    使用idea maven开发spring boot 分布式开发入门
    vertx 从Tcp服务端和客户端开始翻译
    idea中使用github
    gradle多工程依赖
    gradle build scan
    idea 使用方法
    vertx读取配置文件,获得端口号
    支付宝支付
  • 原文地址:https://www.cnblogs.com/E-star/p/3396126.html
Copyright © 2011-2022 走看看