学生实体类,包含姓名和年龄属性,
比较时先按姓名升序排序,如果姓名相同则按年龄升序排序。
第一种:实体类自己实现比较
(实现comparable接口:public interface Comparable<T>,里面就一个方法声明:public int compareTo(T o))
然后利用List类的sort(Comparator<? super E>c)方法或java.util.Collections工具类的sort(List<T>list) (其实里面就一句:list.sort(null))进行排序:
结果:
第二种:借助比较器进行排序。
示例代码:
比较器java.util.Comparator类是一个接口(public interface Comparator<T>),包含int compare(T o1, T o2)等方法:
我们的比较器要实现该接口并实现compare方法:
比较的时候可以利用List的sort(Comparator<? super E>c)方法(或者java.util.Collections工具类的sort(List<T>list, Comparator<? super T>c)方法)进行排序。
结果跟第一种方法一样:
1,使用Comparator 接口Student类 结构如下:(省略getter,setter方法)
public class Student {
/***
* 姓名
*/
private String name
private int age
private String address
/***
* 考试得分
*/
private int score
//省略getter,setter方法
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score
+ "]"
}
}
测试方法:
@Test
public void test_ListComparator(){
List<Student>students=new ArrayList<Student>()
Student stu=null
stu=new Student()
stu.setName("whuang")
stu.setAge(12)
stu.setScore(80)
students.add(stu)
stu=new Student()
stu.setName("rong")
stu.setAge(11)
stu.setScore(90)
students.add(stu)
stu=new Student()
stu.setName("zhu")
stu.setAge(15)
stu.setScore(100)
students.add(stu)
Collections.sort(students,new SystemHWUtil. ListComparator(true,"age"))
System.out.println(students)
}
运行结果:
[Student [name=rong, age=11, score=90], Student [name=whuang, age=12, score=80], Student [name=zhu, age=15, score=100]]
核心类:
public static class ListComparator implements Comparator{
/***
* 是否转化为Int之后再比较
*/
private boolean isConvertInteger
/***
* 对哪个列进行排序
*/
private String comparedProperty
public ListComparator(boolean isConvertInteger,String comparedProperty) {
super()
this.isConvertInteger = isConvertInteger
this.comparedProperty=comparedProperty
}
public int compare(Object o1, Object o2) {
if(null!=o1&&null!=o2)
{
try {
Object obj1=ReflectHWUtils.getObjectValue(o1, comparedProperty)
Object obj2=ReflectHWUtils.getObjectValue(o2, comparedProperty)
if(isConvertInteger){
int num1
int num2
if(obj1 instanceof Integer){
num1=(Integer)obj1
num2=(Integer)obj2
}else{
num1=Integer.parseInt(obj1.toString())
num2=Integer.parseInt(obj2.toString())
}
if(num1>num2){
return 1
}else if(num1<num2){
return -1
}else{
return 0
}
}else{
return obj1.toString().compareTo(obj2.toString())
}
} catch (SecurityException e) {
e.printStackTrace()
} catch (NoSuchFieldException e) {
e.printStackTrace()
} catch (IllegalArgumentException e) {
e.printStackTrace()
} catch (IllegalAccessException e) {
e.printStackTrace()
}
}
return 0/*等于*/
}
}
2,可以指定是升序还是降序
实例:
@Test
public void test_ListComparator(){
List<Student>students=new ArrayList<Student>()
Student stu=null
stu=new Student()
stu.setName("whuang")
stu.setAge(12)
stu.setScore(80)
students.add(stu)
stu=new Student()
stu.setName("rong")
stu.setAge(11)
stu.setScore(90)
students.add(stu)
stu=new Student()
stu.setName("zhu")
stu.setAge(15)
stu.setScore(100)
students.add(stu)
SortList<Student>sortList = new SortList<Student>()
sortList.Sort(students, "getAge", "asc")
System.out.println(students)
}
注意:sortList.Sort 的第二个参数是方法名,不是成员变量名.
核心代码
package com.common.util
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.Collections
import java.util.Comparator
import java.util.List
public class SortList<E>{
public void Sort(List<E>list, final String method, final String sort) {
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0
try {
Method m1 = ((E) a).getClass().getMethod(method, null)
Method m2 = ((E) b).getClass().getMethod(method, null)
if (sort != null &&"desc".equals(sort))// 倒序
ret = m2.invoke(((E) b), null).toString()
.compareTo(m1.invoke(((E) a), null).toString())
else
// 正序
ret = m1.invoke(((E) a), null).toString()
.compareTo(m2.invoke(((E) b), null).toString())
} catch (NoSuchMethodException ne) {
System.out.println(ne)
} catch (IllegalAccessException ie) {
System.out.println(ie)
} catch (InvocationTargetException it) {
System.out.println(it)
}
return ret
}
})
}
}
根据字符串的含义,进行对象化,比如,Student,有三个属性,序号,姓名,分数注意重写Student的Compareable接口
然后,List<String>变成List<Student>students=new ArrayList<Student>
然后,遍历list,算出平均分,放入新的SortList<Student>
打印结果