for example for example List<People> list = ...; //假设有个保存了People类的List Collections.sort(list, new Comparator<People>() { //排序 public int compare(People p1, People p2) { if (p1.getName().compareTo(p2.getName()) == 0) { //如果名字相同 return p2.getAge() - p1.getAge(); //年龄降序 } return p1.getName().compareTo(p2.getName()); //姓名升序 } }); //这样list就排序好了,上述假设People类有getName方法获取姓名,getAge方法获取年龄
一时手痒。写个排序。 1楼代码很好看!import java.util.ArrayList; import java.util.Collections; import java.util.List;class Person implements Comparable{ private int age; private String name;
public Person() { // TODO Auto-generated constructor stub age=0; name=""; } Person(int age,String name){ this.age = age; this.name =name; } public String getName() { return name; } public int getAge() { return age; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub Person p = (Person)o; if(this.name.compareTo(p.getName())>0){ return 1; } else if(this.name.compareTo(p.getName())<0){ return -1; } else{ if(this.age>p.getAge()){ return 1; } else if(this.age<p.getAge()){ return -1; } }
return 0; }
}public class TestSort { public static void main(String[] args) { List<Person> list = new ArrayList<Person>() ; list.add(new Person(31,"zhangsan")); list.add(new Person(13,"lisi")); list.add(new Person(24,"wangwu")); list.add(new Person(24,"lisi"));
Collections.sort(list);
for (Object o : list) { Person p = (Person)o; System.out.println(p.getName()+" "+p.getAge()); } } }
参照1L的代码 重载compare 分别使用Collections.sort
第一次输出前排序一次(按名字) 第二次输出前再排序一次(按年龄) for example//第一次排序 Collections.sort(list, new Comparator<People> () { public int compare(Pleple p1, People p2) { return p1.getName().compareTo(p2.getName()); //按名字排序 } }); //第一次输出//第二次排序 Collections.sort(list, new Comparator<People> () { public int compare(Pleple p1, People p2) { return p1.getAge() - p2.getAge(); //按年龄排序 } }); //第二次输出
我也刚开始学,写了一个,分享下。 public class SortList { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<Person1> list = new ArrayList<Person1>(); Person1 p1 = new Person1(1,"zp"); Person1 p2 = new Person1(2,"apf"); Person1 p3 = new Person1(3,"zpf1"); Person1 p4 = new Person1(4,"bpf"); Person1 p5 = new Person1(5,"zpf3"); Person1 p6 = new Person1(6,"zpf45"); list.add(p1); list.add(p2); list.add(p3); list.add(p4); list.add(p5); list.add(p6); Collections.sort(list, new ComparatorByName()); for(Person1 p : list){ System.out.println(p.getAge()+p.getName()); } } }
class Person1{ int age ; String 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 Person1(int age, String name) { super(); this.age = age; this.name = name; } public Person1() { super(); // TODO Auto-generated constructor stub } }class ComparatorByName implements Comparator{ @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub Person1 p1 = (Person1) o1; Person1 p2 = (Person1) o2; String str1 = p1.getName(); String str2 = p2.getName(); byte[] bt1 = p1.getName().getBytes(); byte[] bt2 = p2.getName().getBytes(); if(str1.length()>str2.length()){ for(int i = 0; i < str2.length();i++){ if(bt1[i]==bt2[i]) continue; return bt1[i] - bt2[i]; } return 1; }else if(str1.length() == str2.length()){ for(int i = 0; i < str2.length();i++){ if(bt1[i] == bt2[i]) continue; return bt1[i] - bt2[i]; } return 0; }else{ for(int i = 0; i < str1.length(); i++){ if(bt1[i] == bt2[i]) continue; return bt1[i] - bt2[i]; } return -1; }
for example
List<People> list = ...; //假设有个保存了People类的List
Collections.sort(list, new Comparator<People>() { //排序
public int compare(People p1, People p2) {
if (p1.getName().compareTo(p2.getName()) == 0) { //如果名字相同
return p2.getAge() - p1.getAge(); //年龄降序
}
return p1.getName().compareTo(p2.getName()); //姓名升序
}
});
//这样list就排序好了,上述假设People类有getName方法获取姓名,getAge方法获取年龄
1楼代码很好看!import java.util.ArrayList;
import java.util.Collections;
import java.util.List;class Person implements Comparable{
private int age;
private String name;
public Person() {
// TODO Auto-generated constructor stub
age=0;
name="";
}
Person(int age,String name){
this.age = age;
this.name =name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Person p = (Person)o;
if(this.name.compareTo(p.getName())>0){
return 1;
}
else if(this.name.compareTo(p.getName())<0){
return -1;
}
else{
if(this.age>p.getAge()){
return 1;
}
else if(this.age<p.getAge()){
return -1;
}
}
return 0;
}
}public class TestSort {
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>() ;
list.add(new Person(31,"zhangsan"));
list.add(new Person(13,"lisi"));
list.add(new Person(24,"wangwu"));
list.add(new Person(24,"lisi"));
Collections.sort(list);
for (Object o : list) {
Person p = (Person)o;
System.out.println(p.getName()+" "+p.getAge());
}
}
}
第二次输出前再排序一次(按年龄)
for example//第一次排序
Collections.sort(list, new Comparator<People> () {
public int compare(Pleple p1, People p2) {
return p1.getName().compareTo(p2.getName()); //按名字排序
}
});
//第一次输出//第二次排序
Collections.sort(list, new Comparator<People> () {
public int compare(Pleple p1, People p2) {
return p1.getAge() - p2.getAge(); //按年龄排序
}
});
//第二次输出
public class SortList { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Person1> list = new ArrayList<Person1>();
Person1 p1 = new Person1(1,"zp");
Person1 p2 = new Person1(2,"apf");
Person1 p3 = new Person1(3,"zpf1");
Person1 p4 = new Person1(4,"bpf");
Person1 p5 = new Person1(5,"zpf3");
Person1 p6 = new Person1(6,"zpf45");
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list.add(p6);
Collections.sort(list, new ComparatorByName());
for(Person1 p : list){
System.out.println(p.getAge()+p.getName());
}
}
}
class Person1{
int age ;
String 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 Person1(int age, String name) {
super();
this.age = age;
this.name = name;
}
public Person1() {
super();
// TODO Auto-generated constructor stub
}
}class ComparatorByName implements Comparator{ @Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Person1 p1 = (Person1) o1;
Person1 p2 = (Person1) o2;
String str1 = p1.getName();
String str2 = p2.getName();
byte[] bt1 = p1.getName().getBytes();
byte[] bt2 = p2.getName().getBytes();
if(str1.length()>str2.length()){
for(int i = 0; i < str2.length();i++){
if(bt1[i]==bt2[i]) continue;
return bt1[i] - bt2[i];
}
return 1;
}else if(str1.length() == str2.length()){
for(int i = 0; i < str2.length();i++){
if(bt1[i] == bt2[i]) continue;
return bt1[i] - bt2[i];
}
return 0;
}else{
for(int i = 0; i < str1.length(); i++){
if(bt1[i] == bt2[i]) continue;
return bt1[i] - bt2[i];
}
return -1;
}
}
}