java 对象排序
1. Comparator类,实现对象的对比字段
class Comparable implements Comparator<P>{
// 对象的排序方式[升、降]
public static boolean sortASC = true;
// 对象的排序属性
public static boolean sortByName = false;
public static boolean sortById= false;
@Override
public int compare(P o1, P o2) {
int result = 0 ;
if(sortASC){
if(sortById){
result = o1.getId().compareTo(o2.getId());
}else if(sortByName){
result = o1.getName().compareTo(o2.getName());
}
}else{
if(sortById){
result = -o1.getId().compareTo(o2.getId());
}else if(sortByName){
result = -o1.getName().compareTo(o2.getName());
}
}
return result;
}
}
2. main方法,简单的对属性排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestMain0630 {
public static void main(String[] args) {
P p1 = new P();
p1.setId(1);
p1.setName("张三");
P p2 = new P();
p2.setId(2);
p2.setName("李四");
P p3 = new P();
p3.setId(3);
p3.setName("王五");
List<P> lp = new ArrayList<P>();
lp.add(p2);
lp.add(p1);
lp.add(p3);
for(P p : lp){
System.out.println(p.getId()+" "+p.getName());
}
Comparable ca = new Comparable();
ca.sortASC=false;
ca.sortById=true;
System.out.println("==========================");
Collections.sort(lp, ca);
for(P p : lp){
System.out.println(p.getId()+" "+p.getName());
}
}
}
class P{
public P(){}
int id;
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;
}
}
tips A:上面的代码,是可以实现效果的,但是,代码的耦合度很高,不利于代码的修改和维护。
比如:如果我们增加了一个属性“生日”,我们希望按照“生日”来排序,那么,我们按照这样的逻辑,我们就必须得修改我们的Comparable代码,明显不符合“开闭原则”。
这里,如果使用“策略模式”,那就再好不过了!
首先,我们得为每个排序行为编写为单独的一个类
class ComparableByIdAsc implements Comparator<P>{
@Override
public int compare(P o1, P o2) {
return o1.getId().compareTo(o2.getId());
}
}
class ComparableByIdDesc implements Comparator<P>{
@Override
public int compare(P o1, P o2) {
return -o1.getId().compareTo(o2.getId());
}
}
然后,我们排序的调用入口
System.out.println("按照id的正序排序");
java.util.Comparator cp = new ComparableByIdAsc();
Collections.sort(lp,cp);
for(P p : lp){
System.out.println(p.getId()+" "+p.getName());
}
System.out.println("按照id的倒序排序");
cp = new ComparableByIdAsc();
Collections.sort(lp,cp);
for(P p : lp){
System.out.println(p.getId()+" "+p.getName());
}
tips B:如果我们希望对map的集合排序呢,该怎么办?即对List<Map>排序
其实,是一样的
public class Lsort {
public static void main(String[] args) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("id", 1);
map.put("name", "1zhangsan");
map.put("age", 18);
Map<String,Object> map2 = new HashMap<String, Object>();
map2.put("id", 2);
map2.put("name", "99zhangsan");
map2.put("age", 20);
Map<String,Object> map3 = new HashMap<String, Object>();
map3.put("id", 3);
map3.put("name", "2zhangsan");
map3.put("age", 22);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
list.add(map2);
list.add(map);
list.add(map3);
Collections.sort(list,new sortBy("name"));
/*Collections.sort(list,new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> last, Map<String, Object> next) {
Object lastSort = last.get("id");
Object nextSort = next.get("id");
if (lastSort == null || nextSort == null) {
return 0;
} else {
return Integer.parseInt(String.valueOf(lastSort))
> Integer.parseInt(String.valueOf(nextSort)) ? 1 : 0;
}
}
});*/
System.out.println(list);
}
}
class sortBy implements Comparator<Map<String,Object>>{
private String by;
sortBy(String by){
this.by = by;
}
@Override
public int compare(Map<String, Object> last, Map<String, Object> next) {
Object lastSort = last.get(by);
Object nextSort = next.get(by);
if (lastSort == null || nextSort == null) {
return 0;
} else {
return lastSort.toString().compareTo(nextSort.toString());
}
}
}