你这问题描述不清 Point implements Comparable<Point>{ public int x; public int y; getter/setter.... int compareTo(Point p){ return p.x - x;//以x坐标排序 } }Point[] points = new Point[10]; //initial....for(Point p : points) { System.err.println(p.x); } 类似这样的东西,你自己改吧。
import java.util.*;public class CompType implements Comparable<CompType> {//数组中的元素实现Comparable接口 private int i, j; public CompType(int i, int j) { this.i = i; this.j = j; } public String toString() {//这个方法无关紧要,我只是为了输出形式美观一点而重写它,如果你不重写,你可以利用for循环打印数组元素 return "[" + i + "," + j + "]"; } public int compareTo(CompType rv) {//实现这个接口方法,用于数组中元素优先级的比较,我只考虑i的大小 return i < rv.i ? -1 : (i == rv.i ? 0 : 1); } public static void main(String[] args) { CompType[] a = { new CompType(1, 3), new CompType(5, 5), new CompType(4, 4), new CompType(2, 1), new CompType(3, 2), new CompType(4, 7), new CompType(6, 7), new CompType(7, 3), new CompType(1, 6), new CompType(1, 1), }; System.out.println("before sort:" + Arrays.toString(a)); Arrays.sort(a); System.out.println("after sort:" + Arrays.toString(a)); } }// //结果: //before sort:[[1,3], [5,5], [4,4], [2,1], [3,2], [4,7], [6,7], [7,3], [1,6], [1,1]] //after sort:[[1,3], [1,6], [1,1], [2,1], [3,2], [4,4], [4,7], [5,5], [6,7], [7,3]]import java.util.*;public class CompType{ private int i, j; public CompType(int i, int j) { this.i = i; this.j = j; } public String toString() {//这个方法无关紧要,我只是为了输出形式美观一点而重写它,如果你不重写,你可以利用for循环打印数组元素 return "[" + i + "," + j + "]"; } class CmpTypeComparator implements Comparator<CompType> { public int compare(CompType o1, CompType o2) {//实现这个接口方法,用于数组中元素优先级的比较,我同时考虑了i,j if(o1.i<o2.i) return -1; else if(o1.i==o2.i) { if(o1.j<o2.j) return -1; else if(o1.j==o2.j)return 0; } return 1; } } public static void main(String[] args) { CompType[] a = { new CompType(1, 3), new CompType(5, 5), new CompType(4, 4), new CompType(2, 1), new CompType(3, 2), new CompType(4, 7), new CompType(6, 7), new CompType(7, 3), new CompType(1, 6), new CompType(1, 1), }; System.out.println("before sort:" + Arrays.toString(a)); Arrays.sort(a,new CompType(0,0).new CmpTypeComparator()); System.out.println("after sort:" + Arrays.toString(a)); } }// //结果: //before sort:[[1,3], [5,5], [4,4], [2,1], [3,2], [4,7], [6,7], [7,3], [1,6], [1,1]] //after sort:[[1,1], [1,3], [1,6], [2,1], [3,2], [4,4], [4,7], [5,5], [6,7], [7,3]]
先放进去,然后再用自定义的比较器进行排序就可以了,Collections里面的sort(List<T> list, Comparator<? super T> c)方法
不是get(),就是下标[],然后强制转换,再.方法名()就行了
胡乱写了下public class User { private int id; private String username; private String password; private String email; public User() { }
public User(int id,String username,String password,String email){ this.id = id; this.username = username; this.password = password; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }
} public class Test { public static List<User> addUser(){ // 生成三个对象 User u1 = new User(1,"Tom","tom","[email protected]"); User u2 = new User(2,"Lucy","Lucy","[email protected]"); User u22 = new User(2,"Gaga","Gaga","[email protected]"); User u3 = new User(3,"Lilei","Lilei","[email protected]");
你这问题描述不清
Point implements Comparable<Point>{
public int x;
public int y; getter/setter.... int compareTo(Point p){
return p.x - x;//以x坐标排序
}
}Point[] points = new Point[10];
//initial....for(Point p : points) {
System.err.println(p.x);
}
类似这样的东西,你自己改吧。
要么 实现一个 Comparator 使用 Arrays.sort(array, comparator) 排序。
import java.util.*;public class CompType implements Comparable<CompType> {//数组中的元素实现Comparable接口
private int i, j;
public CompType(int i, int j) {
this.i = i;
this.j = j;
}
public String toString() {//这个方法无关紧要,我只是为了输出形式美观一点而重写它,如果你不重写,你可以利用for循环打印数组元素
return "[" + i + "," + j + "]";
}
public int compareTo(CompType rv) {//实现这个接口方法,用于数组中元素优先级的比较,我只考虑i的大小
return i < rv.i ? -1 : (i == rv.i ? 0 : 1);
}
public static void main(String[] args) {
CompType[] a = {
new CompType(1, 3), new CompType(5, 5),
new CompType(4, 4), new CompType(2, 1),
new CompType(3, 2), new CompType(4, 7),
new CompType(6, 7), new CompType(7, 3),
new CompType(1, 6), new CompType(1, 1), };
System.out.println("before sort:" + Arrays.toString(a));
Arrays.sort(a);
System.out.println("after sort:" + Arrays.toString(a));
}
}//
//结果:
//before sort:[[1,3], [5,5], [4,4], [2,1], [3,2], [4,7], [6,7], [7,3], [1,6], [1,1]]
//after sort:[[1,3], [1,6], [1,1], [2,1], [3,2], [4,4], [4,7], [5,5], [6,7], [7,3]]import java.util.*;public class CompType{
private int i, j;
public CompType(int i, int j) {
this.i = i;
this.j = j;
}
public String toString() {//这个方法无关紧要,我只是为了输出形式美观一点而重写它,如果你不重写,你可以利用for循环打印数组元素
return "[" + i + "," + j + "]";
}
class CmpTypeComparator implements Comparator<CompType> {
public int compare(CompType o1, CompType o2) {//实现这个接口方法,用于数组中元素优先级的比较,我同时考虑了i,j
if(o1.i<o2.i) return -1;
else if(o1.i==o2.i) {
if(o1.j<o2.j) return -1;
else if(o1.j==o2.j)return 0;
}
return 1;
}
}
public static void main(String[] args) {
CompType[] a = {
new CompType(1, 3), new CompType(5, 5),
new CompType(4, 4), new CompType(2, 1),
new CompType(3, 2), new CompType(4, 7),
new CompType(6, 7), new CompType(7, 3),
new CompType(1, 6), new CompType(1, 1), };
System.out.println("before sort:" + Arrays.toString(a));
Arrays.sort(a,new CompType(0,0).new CmpTypeComparator());
System.out.println("after sort:" + Arrays.toString(a));
}
}//
//结果:
//before sort:[[1,3], [5,5], [4,4], [2,1], [3,2], [4,7], [6,7], [7,3], [1,6], [1,1]]
//after sort:[[1,1], [1,3], [1,6], [2,1], [3,2], [4,4], [4,7], [5,5], [6,7], [7,3]]
private String username;
private String password;
private String email; public User() {
}
public User(int id,String username,String password,String email){
this.id = id;
this.username = username;
this.password = password;
this.email = email;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
}
}
public class Test { public static List<User> addUser(){
// 生成三个对象
User u1 = new User(1,"Tom","tom","[email protected]");
User u2 = new User(2,"Lucy","Lucy","[email protected]");
User u22 = new User(2,"Gaga","Gaga","[email protected]");
User u3 = new User(3,"Lilei","Lilei","[email protected]");
List<User> users = new ArrayList<User>();
// 把对象装进集合中
users.add(u1);
users.add(u2);
users.add(u22);
users.add(u3);
// 使用Collections.sort() 方法进行排序,自定义排序方式,先按id排序,如果id相等,再按username排序
Collections.sort(users,new Comparator<User>() {// 采用匿名类的方式
@Override
public int compare(User o1, User o2) {
int idFlag = o1.getId()-o2.getId();
if(idFlag >= 0){
return o1.getUsername().compareTo(o2.getUsername());// 这个方法就能对字母按字典顺序排序
}
return 0;
}
});
return users;
}
public static void main(String[] args) {
List<User> users = addUser();
for (User user : users) {
System.out.println(user.getId()+" "+user.getUsername()+" "+user.getPassword()+" "+user.getPassword());
}
}
}
1 Tom tom tom
2 Gaga Gaga Gaga
2 Lucy Lucy Lucy
3 Lilei Lilei Lilei