实现集合排序的方法为以下两种:
1.集合中的对象实现Comparable接口,在方法compareTo()中实现比较逻辑
2.新建一个比较器,这个比较器实现Comparator接口,在方法compare()中实现比较逻辑
第一种方法:实现Comparable接口
Person.java代码如下:
package com.compare.bean;
public class Person implements Comparable<Person>{
private int num;
private int weight;
private int score;
public Person(int num,int weight,int score){
this.num = num;
this.weight = weight;
this.score = score;
}
public int compareTo(Person o) {
if(this.score>o.score){
return 1;
}else {
return -1;
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String toString(){
return "Score: "+score+",Weight: "+weight+",Num: "+num;
}
}
Test.java代码
package com.compare.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.compare.bean.Person;
public class Test {
public static void main(String[] args) {
testComparable();
}
public static void testComparable(){
List<Person> persons = new ArrayList<Person>();
persons.add(new Person(1,2,3));
persons.add(new Person(2,2,3));
persons.add(new Person(1,3,4));
persons.add(new Person(3,2,3));
persons.add(new Person(5,2,3));
Collections.sort(persons);
if(persons!=null&&persons.size()>0){
for(Person person:persons){
System.out.println(person);
System.out.println("========================");
}
}
}
}
运行结果:
Score: 3,Weight: 2,Num: 5
========================
Score: 3,Weight: 2,Num: 3
========================
Score: 3,Weight: 2,Num: 2
========================
Score: 3,Weight: 2,Num: 1
========================
Score: 4,Weight: 3,Num: 1
========================
第二种方法:实现Comparator接口
Step.java代码如下:
package com.compare.bean;
public class Step {
private String acceptTime = "";
private String acceptAddress = "";
public Step(){
super();
}
public Step(String acceptTime,String acceptAddress){
this.acceptTime = acceptTime;
this.acceptAddress = acceptAddress;
}
public String getAcceptTime() {
return acceptTime;
}
public void setAcceptTime(String acceptTime) {
this.acceptTime = acceptTime;
}
public String getAcceptAddress() {
return acceptAddress;
}
public void setAcceptAddress(String acceptAddress) {
this.acceptAddress = acceptAddress;
}
}
StepComparator.java代码如下:
package com.compare.util;
import java.util.Comparator;
import java.util.Date;
import com.compare.bean.Step;
public class StepComparator implements Comparator<Step>{
public int compare(Step o1, Step o2) {
Date acceptTime1 = UtilTool.strToDate(o1.getAcceptTime(), "yyyy-MM-dd");
Date acceptTime2 = UtilTool.strToDate(o2.getAcceptTime(), "yyyy-MM-dd");
if(acceptTime1.after(acceptTime2))
return 1;
return -1;
}
}
UtilTool.java代码如下:
package com.compare.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UtilTool {
public static Date strToDate(String str,String formate){
DateFormat df = new SimpleDateFormat(formate);
Date d = null;
try{
d = df.parse(str);
}catch (Exception e) {
e.printStackTrace();
}
return d;
}
}
Test.java代码如下:
package com.compare.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.compare.bean.Step;
import com.compare.util.StepComparator;
public class Test {
public static void main(String[] args) {
testComparator();
}
public static void testComparator(){
List<Step> steps = new ArrayList<Step>();
steps.add(new Step("1997-12-03","HONGKONG"));
steps.add(new Step("1997-12-04","BEIJING"));
steps.add(new Step("1997-11-04","SHANGHAI"));
StepComparator comparator = new StepComparator();
Collections.sort(steps,comparator);
if(steps!=null&&steps.size()>0){
for(Step step:steps){
System.out.println(step.getAcceptTime());
System.out.println(step.getAcceptAddress());
System.out.println("==================");
}
}
}
}
运行结果如下:
1997-11-04
SHANGHAI
==================
1997-12-03
HONGKONG
==================
1997-12-04
BEIJING
==================