使用Comparator 接口 排序

时间:2020-11-28 02:53:13
导读:
  
  import java.util.*;
  public class Mian {
  
  public static void main(String [] age){
  ArrayList d=new ArrayList();
  String []ds=new String[]{"01","2","02","0","3"};
  d.add("01");
  d.add("2");
  d.add("0");
  Collections.sort(d,new Compd());
  Arrays.sort(ds,new Compd());
  System.out.println(d);
  for(int i=0;i   {
  System.out.println(ds[i]);
  }
  
  }
  }
  /**
  * 排序类
  * @author cdy
  * @version 1.0
  */
  class Compd implements Comparator {
  public int compare(Object firstDog, Object secondDog) {
  
  if(firstDog instanceof String){
  String firstHeight = (String)firstDog;
  String secondHeight = (String)secondDog;
  return signum(firstHeight.compareTo(secondHeight));
  }
  
  return 0;
  }
  public static final int signum(int diff) {
  if (diff >0)
  return -1; //大于排前
  if (diff <0)
  return 1;
  else
  return 0;
  }
  
  }
  分类:JAVA
  评论()引用阅读()圈子编辑打印有奖举报
  前一篇:
;i++)>