List集合中元素排序


應用場景:

  在開發中經常遇到要對List<Object>集合進行排序,並且是根據集合中的對象的某個屬性來進行排序    --------以下就此做出的解決方案

 

    public static final String DESC = "desc"; 
     public static final String ASC = "asc"; 
    
    
    /**
     * 用途:對一個List集合數組進行排序
     * 
     * 說明:
     *         目前可以對List<java.lang.Class>、List<POJO>、List<Map>這三種類型集合進行排序
     * 
     * @param list       排序操作的集合對象
     * @param property  指定集合中元素的排序字段,如果集合元素不是對象類型可以傳值為null 
     * @param sort        用於指定是升序還是降序     CollectionsUtil.DESC--降序      CollectionsUtil.ASC--升序
     * @date 2018.04.27 PM
     */
    public static <T> void sortOnList(List<T> list,final Object property,final String sort){
        Collections.sort(list,new Comparator<T>(){
            @Override
            public int compare(T o1, T o2) {
                Integer a1 = null;
                Integer a2 = null;
                if(o1 instanceof Integer) {  //針對List<Integer>
                    a1 = (Integer) o1;
                    a2 = (Integer) o2;
                } else if(o1 instanceof String) { //針對List<String>
                    a1 = Integer.valueOf(o1.toString());
                    a2 = Integer.valueOf(o2.toString());
                } else if(o1 instanceof Map) {   //針對List<Map<String,String>類型
                    Map temp1 = (Map) o1;
                    Map temp2 = (Map) o2;
                    Object object = temp1.get(property);
                    if(object instanceof Integer) { 
                        a1 = (Integer) object;
                        a2 = (Integer) temp2.get(property);
                    } else if(object instanceof String){ //根據Map中value來進行排序String類型需要轉換
                        a1 = Integer.parseInt(object.toString());
                        a2 = Integer.parseInt(temp2.get(property).toString());
                    }
                } else  {   //針對對象類型
                    Class c1 = o1.getClass();
                    Class c2 = o2.getClass();
                    try {
                        Field declaredField1 = c1.getDeclaredField(property.toString());
                        Field declaredField2 = c2.getDeclaredField(property.toString());
                        declaredField1.setAccessible(true);  
                        declaredField2.setAccessible(true);  
                        a1 = Integer.parseInt(declaredField1.get(o1).toString());  
                        a2 = Integer.parseInt(declaredField2.get(o2).toString());  
                    } catch (Exception e) {
                        e.printStackTrace();
                    } 
                }
                if(sort.equals(CollectionsUtil.ASC))   //升序
                    return a1.compareTo(a2);
                else                                   //降序
                    return a2.compareTo(a1);
            }
        });
    }

 

 

 

以下代碼原理:

 

  Collections.sort(list,new Comparator<T>(){
       @Override
            public int compare(T o1, T o2){
       } 
  }
根據集合中的元素,把o1與o2對象的某個數值型屬性進行對比:
    o1 - o2 >= 0 --- 升序
    o1 - o2 < 0    ---  降序


  

 

sort方法是對List集合中對象某個屬性來進行排序的,例如:

package hello1;

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

import hello.Student;

public class Main {
    
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setId(6);
        
        Student s2 = new Student();
        s2.setId(66);
        
        Student s3 = new Student();
        s3.setId(1);
        
        Student s4 = new Student();
        s4.setId(55);
        
        List<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        
        System.out.println("未排序結果:"+ list);
        
        
        //根據Student對象id屬性排序
        Collections.sort(list, new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                return o1.getId() - o2.getId();
            }
            
        });
        
        System.out.println("排序后結果:"+ list);
        
    }

}

 

運行結果 ======================》

                                                                               

 

 

歡迎路過的哥們提好建議

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM