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

}

  

 

 

posted @ 2013-10-30 10:19  E_star  阅读(564)  评论(0编辑  收藏  举报