这篇文章主要介绍了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)是按照枚举类型值在定义时的先后顺序比较的,越后面的越大,
而不是按照值的字母先后顺序比较的。
以上就是java 实现list排序的默认方法和自定义方法的详细内容。