java Collections.sort()实现List排序的默认方法和自定义方法

阅读更多
  • Java提供的默认list排序方法

相应代码:

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 创建时间:2017-1-14
 * 
 * @author Dsz
 */
public class testListSort1 {

	public static void main(String[] args) {
		List list = new ArrayList<>();
		list.add("刘媛媛");

		list.add("王硕");
		list.add("李明");
		list.add("刘迪");
		list.add("刘布");

		// 升序
		// 注意:是根据的汉字的拼音的字母排序的,而不是根据汉字一般的排序方法
		Collections.sort(list, Collator.getInstance(java.util.Locale.CHINA));
		System.out.println("升序:");
		for (int i = 0; i < list.size(); i++) {
			System.out.print(list.get(i) + " ");
		}
		System.out.println("");

		// 降序
		// 不指定排序规则时,也是按照字母的来排序的
		Collections.reverse(list);
		System.out.println("降序:");
		for (int i = 0; i < list.size(); i++) {
			System.out.print(list.get(i) + " ");
		}
	}
}

输出结果:

升序:
李明 刘布 刘迪 刘媛媛 王硕 
降序:
王硕 刘媛媛 刘迪 刘布 李明 
  • 自定义的排序规则

第一种是model类实现Comparable接口,重写重写int compareTo(Object o)方法

model类:

/**
 * 创建时间:2017-1-14
 * 
 * @author Dsz
 */
public class Student implements Comparable {
	private String name;
	private Integer age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public int compareTo(Object o) {

		Student sdto = (Student) o;

		int otherAge = sdto.getAge();
		// note: enum-type's comparation depend on types' list order of enum
		// method
		// so, if compared property is enum-type ,then its comparationfollow
		// ObjEnum.objType order

		return this.age.compareTo(otherAge);
	}
}

操作方法代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 创建时间:2017-1-14
 * 
 * @author Dsz
 */
public class testListSort2 {

	public static void main(String[] args) {
		List studentList = new ArrayList<>();

		Student s1 = new Student();
		s1.setName("yuanyuan");
		s1.setAge(22);
		studentList.add(s1);

		Student s2 = new Student();
		s2.setName("lily");
		s2.setAge(23);
		studentList.add(s2);

		// 按照age升序 22,23,
		Collections.sort(studentList);

		System.out.println("升序:");
		int size = studentList.size();
		for (int i = 0; i < size; i++) {
			String str = toJsonString(studentList.get(i));
			System.out.println(str);
		}

		// 按照age降序 23,22
		Collections.reverse(studentList);

		System.out.println("降序:");
		for (int i = 0; i < size; i++) {
			String str = toJsonString(studentList.get(i));
			System.out.println(str);
		}

	}

	/**
	 * 将object转化为字符串
	 * 
	 * @param obj
	 * @return
	 */
	public static String toJsonString(Object obj) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();

		return gson.toJson(obj);
	}
}

输出结果:

升序:
{"name":"yuanyuan","age":22}
{"name":"lily","age":23}
降序:
{"name":"lily","age":23}
{"name":"yuanyuan","age":22}

 

第二种是比较器类实现Comparator接口,重写int compare(Object o1, Object o2)方法

model类:

/**
 * 创建时间:2017-1-14
 * 
 * @author Dsz
 */
public class StudentDTO {
	private String name;
	private Integer age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}

操作方法与比较类代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 创建时间:2017-1-14
 * 
 * @author Dsz
 */
public class test3ListSort {

	public static void main(String[] args) {
		List studentList = new ArrayList<>();

		StudentDTO s1 = new StudentDTO();
		s1.setName("yuanyuan");
		s1.setAge(22);
		studentList.add(s1);

		StudentDTO s2 = new StudentDTO();
		s2.setName("lily");
		s2.setAge(23);
		studentList.add(s2);

		MyCompartor1 mc1 = new MyCompartor1();
		MyCompartor2 mc2 = new MyCompartor2();

		// 按照age升序 22,23,
		Collections.sort(studentList, mc1);
		System.out.println("升序:");
		int size = studentList.size();
		for (int i = 0; i < size; i++) {
			String str = toJsonString(studentList.get(i));
			System.out.println(str);
		}

		// 按照age降序 23,22
		Collections.sort(studentList, mc2);
		System.out.println("降序:");
		for (int i = 0; i < size; i++) {
			String str = toJsonString(studentList.get(i));
			System.out.println(str);
		}
	}

	/**
	 * 将object转化为字符串
	 * 
	 * @param obj
	 * @return
	 */
	public static String toJsonString(Object obj) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();

		return gson.toJson(obj);
	}
}

/**
 * 比较器类-升序操作
 * 
 * @author Dsz
 * 
 *         2017-1-14
 */
class MyCompartor1 implements Comparator {
	@Override
	public int compare(Object o1, Object o2) {

		StudentDTO sdto1 = (StudentDTO) o1;
		Integer age1 = sdto1.getAge();
		StudentDTO sdto2 = (StudentDTO) o2;
		Integer age2 = sdto2.getAge();

		return age1.compareTo(age2);

	}
}

/**
 * 比较器类-降序操作
 * 
 * @author Dsz
 * 
 *         2017-1-14
 */
class MyCompartor2 implements Comparator {
	@Override
	public int compare(Object o1, Object o2) {

		StudentDTO sdto1 = (StudentDTO) o1;
		Integer age1 = sdto1.getAge();
		StudentDTO sdto2 = (StudentDTO) o2;
		Integer age2 = sdto2.getAge();

		return age2.compareTo(age1);

	}
}

输出结果:

升序:
{"name":"yuanyuan","age":22}
{"name":"lily","age":23}
降序:
{"name":"lily","age":23}
{"name":"yuanyuan","age":22}

附注:

1.对于数组的排序方法如下:

String[] names = {"王林",  "杨宝", "李镇", "刘迪", "刘波"};  
Arrays.sort(names, com.ibm.icu.text.Collator.getInstance(com.ibm.icu.util.ULocale.SIMPLIFIED_CHINESE));//升序;   
System.out.println(Arrays.toString(names));      

2.对于汉字的排序:可以尝试使用ICU4J会得到更好的结果,特别是姓为某些生僻字的时候,

用com.ibm.icu.text.Collator替换java.text.Collator,用com.ibm.icu.util.ULocale替换java.util.Locale

3.对于枚举类型的enum1.compareTo(enum2)是按照枚举类型值在定义时的先后顺序比较的,越后面的越大,

而不是按照值的字母先后顺序比较的。

转载于:https://my.oschina.net/u/2968607/blog/824691

你可能感兴趣的:(java Collections.sort()实现List排序的默认方法和自定义方法)