java Collections.sort()實現List排序的默認方法和自定義方法


1.java提供的默認list排序方法

主要代碼:

List<String> list = new ArrayList();list.add("劉媛媛");

list.add("王碩");
list.add("李明");
list.add("劉迪");
list.add("劉布");

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

//降序
Collections.reverse(list);//不指定排序規則時,也是按照字母的來排序的
for(int i=0;i<list.size();i++)
{
     System.out.print(list.get(i));
}

輸出結果:

李明劉布劉迪劉媛媛王碩
王碩劉媛媛劉迪劉布李明

 

2.自定義的排序規則:

第一種是model類實現Comparable接口,重寫重寫int compareTo(Object o)方法

            model類:

public class StudentDTO implements Comparable
{
 private String name;
 private int age;

public String getName()
{
    return name;
}

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

public ObjType getType()
{
    return type;
}

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

@Override
public int compareTo(Object o)
{

       StudentDTO sdto = (StudentDTO)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);
}
}

           主方法:           

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

      StudentDTO s1 = new StudentDTO ();

      s.setName("yuanyuan");

      s.setAge(22);

      studentList.add(s1);

                  StudentDTO s1 = new StudentDTO ();

                  s.setName("lily");

                  s.setAge(23);

                  studentList.add(s2);

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

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

}

第二種是比較器類實現Comparator接口,重寫int compare(Object o1, Object o2)方法;

           model類:           

public class StudentDTO implements Comparable
{
     private String name;
     private int age;

     public String getName()
     {
         return name;
     }

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

     public ObjType getType()
     {
         return type;
     }

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

           比較器類:

class MyCompartor implements Comparator
{
     @Override
     public int compare(Object o1, Object o2)
    {

           StudentDTO sdto1= (StudentDTO )o1;

           StudentDTO sdto2= (StudentDTO )o2;

           return sdto1.getAge.compareTo(stdo2.getAge())

    }
}

           主方法:

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

      StudentDTO s1 = new StudentDTO ();

      s.setName("yuanyuan");

      s.setAge(22);

      studentList.add(s1);

      StudentDTO s1 = new StudentDTO ();

      s.setName("lily");

      s.setAge(23);

      studentList.add(s2);

      MyComparetor mc = new MyComparetor();

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

      Collections.reverse(studentList,mc);    //按照age降序 23,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)是按照枚舉類型值在定義時的先后順序比較的,越后面的越大,

而不是按照值的字母先后順序比較的。


   

 

    

 


免責聲明!

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



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