List集合对象中按照不同属性大小排序的实例
程序员文章站
2024-03-04 17:50:42
实例如下:
package com.huad.luck;
import java.util.arraylist;
import java.util.col...
实例如下:
package com.huad.luck; import java.util.arraylist; import java.util.collections; import java.util.comparator; import java.util.list; public class test { public static void main(string[] args) { person p = new person(); p.setname("tom"); p.setage(11); person p1 = new person(); p1.setname("lilei"); p1.setage(19); person p2 = new person(); p2.setname("json"); p2.setage(15); list<person> list =new arraylist<person>(); list.add(p); list.add(p1); list.add(p2); collections.sort(list,new comparator<person>() { //这里可以再person中实现 comparator<t>接口,重写compare方法 @override public int compare(person o1, person o2) { //这里按照名字排序 return o1.getname().compareto(o2.getname()); //这里按照age排序 //return (o1.getage()+"").compareto(o2.getage()+""); //。。。根据不同属性值排序 } }); for (int i = 0; i < list.size(); i++) { system.out.println(list.get(i).getname()); } } } //person类 package com.huad.luck; public class person{ private string name ; private int age; public person(string name , int age) { this.name = name; this.age = age; } public person( ) { } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getage() { return age; } public void setage(int age) { this.age = age; } } <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 8px; margin-bottom: 8px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 12.5px; word-wrap: break-word; word-break: break-all; color: rgb(51, 51, 51); font-family: 'microsoft yahei', verdana, sans-serif, 宋体;"><span style="padding: 0px; margin: 0px; letter-spacing: normal; font-stretch: normal; font-size: 14px; line-height: 24px; font-family: tahoma; color: rgb(0, 0, 0);">用collection.sort()方法对list集合排序</span><br style="padding: 0px; margin: 0px;" /></p><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 24px; font-size: 14px; word-wrap: break-word; word-break: break-all; font-stretch: normal; font-family: tahoma;"><span style="padding: 0px; margin: 0px; list-style: none outside none; word-wrap: break-word; word-break: normal;"><span style="padding: 0px; margin: 0px; list-style: none outside none; line-height: 21px; word-wrap: break-word; word-break: normal;"></span></span></p><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 1em; margin-bottom: 0.5em; line-height: 24px; font-size: 14px; word-wrap: break-word; word-break: break-all; font-stretch: normal; font-family: tahoma;">第一种是list中的对象实现comparable接口,如下:<span style="padding: 0px; margin: 0px; list-style: none outside none; word-wrap: break-word; word-break: normal;"> </span></p><pre class="java" style="padding: 5px; margin-top: 0px; margin-bottom: 0px; line-height: 18px; font-size: 9pt; font-family: 'courier new', arial; border: 1px solid rgb(221, 221, 221); color: rgb(51, 51, 51); background: rgb(246, 246, 246);">/** * 根据order对user排序 */ public class user implements comparable<user>{ private string name; private integer order; public string getname() { return name; } public void setname(string name) { this.name = name; } public integer getorder() { return order; } public void setorder(integer order) { this.order = order; } public int compareto(user arg0) { return this.getorder().compareto(arg0.getorder()); } }
测试一下:
public class test{ public static void main(string[] args) { user user1 = new user(); user1.setname("a"); user1.setorder(1); user user2 = new user(); user2.setname("b"); user2.setorder(2); list<user> list = new arraylist<user>(); //此处add user2再add user1 list.add(user2); list.add(user1); collections.sort(list); for(user u : list){ system.out.println(u.getname()); } } }
输出结果如下
a
b
第二种方法是根据collections.sort重载方法来实现,例如:
/** * 根据order对user排序 */ public class user { //此处无需实现comparable接口 private string name; private integer order; public string getname() { return name; } public void setname(string name) { this.name = name; } public integer getorder() { return order; } public void setorder(integer order) { this.order = order; } }
主类中这样写即可:
public class test{ public static void main(string[] args) { user user1 = new user(); user1.setname("a"); user1.setorder(1); user user2 = new user(); user2.setname("b"); user2.setorder(2); list<user> list = new arraylist<user>(); list.add(user2); list.add(user1); collections.sort(list,new comparator<user>(){ public int compare(user arg0, user arg1) { return arg0.getorder().compareto(arg1.getorder()); } }); for(user u : list){ system.out.println(u.getname()); } } }
输出结果如下
a
b
前者代码结构简单,但是只能根据固定的属性排序,后者灵活,可以临时指定排序项,但是代码不够简洁
多字段的场合:
collections.sort(list,new comparator<user>(){ public int compare(user arg0, user arg1) { // 第一次比较专业 int i = arg0.getorder().compareto(arg1.getorder()); // 如果专业相同则进行第二次比较 if(i==0){ // 第二次比较 int j=arg0.getxxx().compareto(arg1.getxxx()); // 如果学制相同则返回按年龄排序 if(j==0){ return arg0.getccc().compareto(arg1.getccc()); } return j; } return i; } });
以下是另外一个例子
package test; import java.util.arraylist; import java.util.collections; import java.util.comparator; public class arraylisttest { public static void main(string[] args) { arraylist<exmployee> arraylist = new arraylist<exmployee>() ; arraylist.add(new exmployee("zengqiang",new integer(5000))) ; arraylist.add(new exmployee("zengmin",new integer(4000))) ; arraylist.add(new exmployee("liuxiaojuan",new integer(4200))) ; arraylist.add(new exmployee("giuming",new integer(2200))) ; collections.sort(arraylist, new comparator<exmployee>(){ public int compare(exmployee arg0, exmployee arg1) { return arg1.getsalary()-arg0.getsalary() ; //按照工资升序 //return arg0.getsalary()-arg1.getsalary() ; 按照工资降序 //return arg0.getname().compareto(arg1.getname()) ; 按照姓名升序 } }); for(exmployee e:arraylist) system.out.println(e.getname()+"'s salary is "+e.getsalary()) ; } } class exmployee { public exmployee(string name, int salary) { this.name = name ; this.salary = salary ; } private string name; public string getname() { return name; } public void setname(string name) { this.name = name; } public int getsalary() { return salary; } public void setsalary(int salary) { this.salary = salary; } private int salary; }
以上这篇list集合对象中按照不同属性大小排序的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。