做java应用开发常常需要对集合进行升降序操作,习惯了C#中LINQ对java中集合操作实在不感冒(PS:不得不吐槽一下,java更新速度太慢了 C#在3.0就加上了LINQ,lamba等新特性,到现在java这些特性还不是标准)
第一步定义一个需要排序的类person
package com.dbc.yangg.main.test;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Person {
String username;
String department;
int id;
double codeId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getId() {
return id;
}
public double getCodeId() {
return codeId;
}
public Person(String username,double codeID,int id,String department){
this.username=username;
this.codeId=codeID;
this.id=id;
this.department=department;
}
public static void sortObj(List<Person> persons,Type type){
sortObj(persons,type,OrderBy.ASC);
}
public static void sortObj(List<Person> persons,Type type,OrderBy orderBy){
Collections.sort(persons, new Compare(type,orderBy));
}
public enum Type{
USERNAME,DEPARTMENT,ID,CODEID;
}
public enum OrderBy{
DESC,ASC;
}
private static class Compare implements Comparator<Person>{
private Type type;
private OrderBy orderBy;
public Compare(Type type,OrderBy orderBy){
this.type=type;
this.orderBy=orderBy;
}
@Override
public int compare(Person arg0, Person arg1) {
// TODO Auto-generated method stub
int result=0;
switch (this.type) {
case USERNAME:
result=arg0.getUsername().compareTo(arg1.getUsername());
break;
case DEPARTMENT:
result=arg0.getDepartment().compareTo(arg1.getDepartment());
break;
case ID:
result=arg0.getId()-(arg1.getId());
break;
case CODEID:
if(arg0.getCodeId()>=arg1.getCodeId()){
result=1;
}else
result=-1;
break;
}
if(orderBy==OrderBy.DESC){
result*=-1;
}
return result;
}
}
}
第二步 编写测试代码
package com.dbc.yangg.main.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import com.dbc.yangg.main.test.Person.OrderBy;
import com.dbc.yangg.main.test.Person.Type;
import sun.awt.util.IdentityArrayList;
public class ThreeHomework {
public static void main(String[] argv){
List<Person> persons=new ArrayList<Person>();
Person person1=new Person("hadoop", 12.5, 3, "iihadoop");
Person person2=new Person("openstack", 25.5, 6, "aopenstack");
Person person3=new Person("hive", 12.4, 9, "bhive");
Person person4=new Person("pig", 8.8, 25, "cpig");
Person person5=new Person("hbase", 5.8, 5, "dhbase");
Person person6=new Person("mongodb", 12.5, 7, "emongodb");
persons.add(person1);
persons.add(person2);
persons.add(person3);
persons.add(person4);
persons.add(person5);
persons.add(person6);
System.out.println(">>>>>>>>>>\t原始数据\t<<<<<<<<<<<<<");
print(persons);
System.out.println(">>>>>>>>>>\tID ADS\t<<<<<<<<<<<<<");
Person.sortObj(persons, Type.ID);
print(persons);
System.out.println(">>>>>>>>>>\tID DESC\t<<<<<<<<<<<<<");
Person.sortObj(persons, Type.ID,OrderBy.DESC);
print(persons);
System.out.println(">>>>>>>>>>\tCODEID\t<<<<<<<<<<<<<");
Person.sortObj(persons, Type.CODEID);
print(persons);
System.out.println(">>>>>>>>>>\tUSERNAME\t<<<<<<<<<<<<<");
Person.sortObj(persons, Type.USERNAME);
print(persons);
System.out.println(">>>>>>>>>>\tDEPARTMENT\t<<<<<<<<<<<<<");
Person.sortObj(persons, Type.DEPARTMENT,OrderBy.DESC);
print(persons);
}
private static void print(List<Person> persons){
System.out.println("ID"+"\t"+"CodeID"+"\t"+"username"+"\t"+"department");
for(Person ps:persons){
System.out.println(ps.getId()+"\t"+ps.getCodeId()+"\t"+ps.getUsername()+"\t\t"+ps.getDepartment());
}
}
}
>>>>>>>>>> 原始数据 <<<<<<<<<<<<<
ID CodeID username department
3 12.5 hadoop iihadoop
6 25.5 openstack aopenstack
9 12.4 hive bhive
25 8.8 pig cpig
5 5.8 hbase dhbase
7 12.5 mongodb emongodb
>>>>>>>>>> ID ADS <<<<<<<<<<<<<
ID CodeID username department
3 12.5 hadoop iihadoop
5 5.8 hbase dhbase
6 25.5 openstack aopenstack
7 12.5 mongodb emongodb
9 12.4 hive bhive
25 8.8 pig cpig
>>>>>>>>>> ID DESC <<<<<<<<<<<<<
ID CodeID username department
25 8.8 pig cpig
9 12.4 hive bhive
7 12.5 mongodb emongodb
6 25.5 openstack aopenstack
5 5.8 hbase dhbase
3 12.5 hadoop iihadoop
>>>>>>>>>> CODEID <<<<<<<<<<<<<
ID CodeID username department
5 5.8 hbase dhbase
25 8.8 pig cpig
9 12.4 hive bhive
3 12.5 hadoop iihadoop
7 12.5 mongodb emongodb
6 25.5 openstack aopenstack
>>>>>>>>>> USERNAME <<<<<<<<<<<<<
ID CodeID username department
3 12.5 hadoop iihadoop
5 5.8 hbase dhbase
9 12.4 hive bhive
7 12.5 mongodb emongodb
6 25.5 openstack aopenstack
25 8.8 pig cpig
>>>>>>>>>> DEPARTMENT <<<<<<<<<<<<<
ID CodeID username department
3 12.5 hadoop iihadoop
7 12.5 mongodb emongodb
5 5.8 hbase dhbase
25 8.8 pig cpig
9 12.4 hive bhive
6 25.5 openstack aopenstack
ok 可以早点睡了 明天继续到公司各种扯皮(~~~~~~生命不止,折腾不休~~~~~~~~)