Home  >  Article  >  Java  >  java Collections.sort() implements the default method and custom method of List sorting

java Collections.sort() implements the default method and custom method of List sorting

巴扎黑
巴扎黑Original
2017-06-26 11:13:511794browse

1.Default list sorting method provided by java

Main code:

List list = new ArrayList();list.add("Liu Yuanyuan ");

list.add("Wang Shuo");
list.add("Li Ming");
list.add("Liu Di");
list. add("Liu Bu");

//Ascending order
Collections.sort(list,Collator.getInstance(java.util.Locale.CHINA));//Note: It is based on the pinyin of the Chinese characters Sorted alphabetically, rather than according to the general sorting method of Chinese characters
for(int i=0;i {
System.out.print(list.get( i));
}
        System.out.println("");

//Descending order
      Collections.reverse(list);//When no sorting rules are specified, it is also in accordance with Sorted alphabetically
                                                                                                                                         .

Output result:

李明Liu Bu Liu Di Liu Yuanyuan Wang Shuo

王 Shuo Liu Yuanyuan Liu Di Liu Bu Li Ming


2. Customized sorting rules:

The first is that the model class implements the Comparable interface and overrides the int compareTo(Object o) method

Model class:

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 comparison depend on types' list order of enum method

// so, if compared property is enum-type ,then its comparisonfollow ObjEnum.objType order



return this.age. compareTo(otherAge);

}

}

Main method:

public static void main(String[] args)

{

List 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); ; //In descending order of age 23, 22

}

The second is that the comparator class implements the Comparator interface and overrides the int compare(Object o1, Object o2) method;

Model:

Public Class Studentdto Implements Comparable

{ Private String name; Prive In Age; AME (AME (AME )

{

        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 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)是按照枚举类型值在定义时的先后顺序比较的,越后面的越大,

而不是按照值的字母先后顺序比较的。


 

The above is the detailed content of java Collections.sort() implements the default method and custom method of List sorting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn