JAva:カスタムソート

3312 ワード

   /**
     *       o1  o2,      ;  o1  o2,      ;      ,   0;
     */
    Comparator comparator_up = new Comparator(){
        @Override
        public int compare(Object s1, Object s2) {
            int o1 = 0;
            int o2 = 0;
            if(s1 instanceof Pop && s2 instanceof Pop){
                try{
                    o1 = Integer.valueOf(((Pop) s1).getStation());
                    o2 = Integer.valueOf(((Pop) s2).getStation());
                }catch (Exception e){
                    return 0;
                }
            }
            if(o1 < o2) return -1;
            if(o1 > o2) return 1;
            return 0;
        }
    };
 /**
     *       o1  o2,      ;  o1  o2,      ;      ,   0;
     */
  Comparator comparator_down = new Comparator(){
        @Override
        public int compare(Object s1, Object s2) {
            int o1 = 0;
            int o2 = 0;
            if(s1 instanceof Pop && s2 instanceof Pop){
                try{
                    o1 = Integer.valueOf(((Pop) s1).getStation());
                    o2 = Integer.valueOf(((Pop) s2).getStation());
                }catch (Exception e){
                    return 0;
                }
            }
            if(o1 < o2) return 1;//
            if(o1 > o2) return -1;
            return 0;
        }
    };

if(order==1){//昇順Collections.sort(list_pop,comparator_up);}else if(order==2){//降順Collections.sort(list_pop,comparator_down);}