概述:
有些场景需要对 数据结构中的数据进行排序,java中提供了sort方法, 可以通过自定义的comparator,指定排序方式,以下为例子及实现
使用
例子1
: comparator 比较 list 中对象属性
package JavaContainer.comparator;//https://www.cnblogs.com/rossoneri/p/4549529.htmlimport org.junit.Test;import java.util.*;//根据姓名排序class ComparatorName implements Comparator{ boolean is_Ascend; public ComparatorName(boolean b) { // TODO Auto-generated constructor stub is_Ascend = b; } @Override public int compare(PersonComparaBean o1, PersonComparaBean o2) { //o1 o2 分别对应容器中前一个对象和后一个对象 if (is_Ascend) return o1.getName().compareTo(o2.getName()); else return o2.getName().compareTo(o1.getName()); }}//根据id排序class ComparatorId implements Comparator { private boolean aescOrDesc; public ComparatorId(boolean aescOrDesc) { this.aescOrDesc = aescOrDesc; } @Override public int compare(PersonComparaBean o1 , PersonComparaBean o2) { int a, b; if (aescOrDesc) { a = o1.getId(); b = o2.getId(); } else { a = o2.getId(); b = o1.getId(); } if (a > b) return 1; else if (a == b) return 0; else return -1; }}public class ComparatorDemo1 { @Test public void sortByNameOrId(){ PersonComparaBean personComparaBean1 = new PersonComparaBean(); personComparaBean1.setId(1); personComparaBean1.setName("ZZZZ"); PersonComparaBean personComparaBean2 = new PersonComparaBean(); personComparaBean2.setId(5); personComparaBean2.setName("CCCC"); PersonComparaBean personComparaBean4 = new PersonComparaBean(); personComparaBean4.setId(2); personComparaBean4.setName("DDDD"); PersonComparaBean personComparaBean3 = new PersonComparaBean(); personComparaBean3.setId(8); personComparaBean3.setName("AAAA"); List list = new ArrayList<>(); list.add(personComparaBean1); list.add(personComparaBean2); list.add(personComparaBean3); list.add(personComparaBean4); for (PersonComparaBean personComparaBean : list){ System.out.println("原: "+personComparaBean); } /* 原: PersonComparaBean{id='1', name='ZZZZ'} 原: PersonComparaBean{id='5', name='CCCC'} 原: PersonComparaBean{id='8', name='AAAA'} 原: PersonComparaBean{id='2', name='DDDD'}*/ Collections.sort(list,new ComparatorId(true)); for (PersonComparaBean personComparaBean : list){ System.out.println("id: "+personComparaBean); } /*id: PersonComparaBean{id='1', name='ZZZZ'} id: PersonComparaBean{id='2', name='DDDD'} id: PersonComparaBean{id='5', name='CCCC'} id: PersonComparaBean{id='8', name='AAAA'}*/ Collections.sort(list,new ComparatorName(true)); for (PersonComparaBean personComparaBean : list){ System.out.println("name: "+personComparaBean); } /*name: PersonComparaBean{id='8', name='AAAA'} name: PersonComparaBean{id='5', name='CCCC'} name: PersonComparaBean{id='2', name='DDDD'} name: PersonComparaBean{id='1', name='ZZZZ'}*/ }}
例子2
: 比较二位数组 comparator
package JavaContainer.comparator;import org.junit.Test;import java.util.Arrays;import java.util.Comparator;//二维数组第二位比较(如果想要先判断第一位,然后第二位,可以在比较器中再加判断条件对比)public class ComparatorDemo2 { @Test public void test() { int[][] nums = { {1, 2}, {2, 3}, {1, 1}}; for (int[] num : nums) { for (int num1 : num) { System.out.print(num1); } System.out.println(" "); } Arrays.sort(nums, new Comparator() { @Override public int compare(int[] o1, int[] o2) { return o1[1] - o2[1]; } }); for (int[] num : nums) { for (int num1 : num) { System.out.print(num1); } System.out.println(" "); } /*12 23 11 11 12 23 */ }}