对集合重新排序(限于基本类型数据)

 
package com.jccw.util; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortList < E > { @SuppressWarnings( " unchecked " ) public void sortList(List < E > list, final String method, final String sort){ final String name = " get " + method.substring( 0 , 1 ).toUpperCase() + method.substring( 1 ); Collections.sort(list, new Comparator(){ public int compare(Object a,Object b){ int i = 0 ; try { Method m1 = ((E)a).getClass().getMethod(name, null ); Method m2 = ((E)b).getClass().getMethod(name, null ); Object o1 = m1.invoke(((E)a), null ); Object o2 = m2.invoke(((E)b), null ); String typeName = o1.getClass().getSimpleName(); if (typeName.equals( " Integer " ) || typeName.equals( " Long " ) || typeName.equals( " Double " ) || typeName.equals( " Short " ) || typeName.equals( " Float " )){ i = compareTo(typeName,o1,o2); if (sort != null && " desc " .equals(sort)){ i = i == 0 ? i: - i; } else { i = i == 0 ? i:i; } } else { if (sort != null && " desc " .equals(sort)){ i = o2.toString().compareTo(o1.toString()); } else { i = o1.toString().compareTo(o2.toString()); } } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; } }); } public int compareTo(String typeName,Object a,Object b){ if (typeName.equals( " Integer " )){ int i1 = (Integer)a; int i2 = (Integer)b; if (i1 == i2) return 0 ; else return i1 - i2 > 0 ? 1 : - 1 ; } else if (typeName.equals( " Long " )){ long i1 = (Long)a; long i2 = (Long)b; if (i1 == i2) return 0 ; else return i1 - i2 > 0 ? 1 : - 1 ; } else if (typeName.equals( " Double " )){ double i1 = (Double)a; double i2 = (Double)b; if (i1 == i2) return 0 ; else return i1 - i2 > 0 ? 1 : - 1 ; } else if (typeName.equals( " Short " )){ short i1 = (Short)a; short i2 = (Short)b; if (i1 == i2) return 0 ; else return i1 - i2 > 0 ? 1 : - 1 ; } else if (typeName.equals( " Float " )){ float i1 = (Float)a; float i2 = (Float)b; if (i1 == i2) return 0 ; else return i1 - i2 > 0 ? 1 : - 1 ; } else { return 0 ; } } public static void main(String[] args) { Student stu1 = new Student( 1 , " aaa1 " ,99F, 99L , 99 ,99D,( short ) 99 , " 000 " ); Student stu2 = new Student( 2 , " aaa2 " ,49F, 49L , 49 ,49D,( short ) 49 , " 000 " ); Student stu3 = new Student( 3 , " aaa3 " ,33F, 33L , 33 ,33D,( short ) 33 , " 000 " ); Student stu4 = new Student( 4 , " aaa4 " ,51F, 51L , 51 ,51D,( short ) 51 , " 000 " ); Student stu5 = new Student( 5 , " aaa5 " ,43F, 43L , 43 ,43D,( short ) 43 , " 000 " ); Student stu6 = new Student( 6 , " aaa6 " ,65F, 65L , 65 ,65D,( short ) 65 , " 000 " ); List < Student > list = new ArrayList < Student > (); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); list.add(stu6); System.out.println(System.currentTimeMillis()); SortList < Student > sort = new SortList < Student > (); sort.sortList(list, " name " , " asc " ); System.out.println(System.currentTimeMillis()); for (Student s : list) { System.out.println(s.getId() + " " + s.getName() + " " + s.getSdouble() + " " + s.getSfloat() + " " + s.getSint() + " " + s.getSlong() + " " + s.getSshort()); } } } class Student{ private int id; private String name; private float sfloat; private long slong; private int sint; private short sshort; private double sdouble; private String sex; public Student() { // TODO Auto-generated constructor stub } public Student( int id, String name, float sfloat, long slong, int sint, double sdouble, short sshort, String sex) { this .id = id; this .name = name; this .sfloat = sfloat; this .slong = slong; this .sint = sint; this .sdouble = sdouble; this .sshort = sshort; this .sex = sex; } public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public float getSfloat() { return sfloat; } public void setSfloat( float sfloat) { this .sfloat = sfloat; } public long getSlong() { return slong; } public void setSlong( long slong) { this .slong = slong; } public int getSint() { return sint; } public void setSint( int sint) { this .sint = sint; } public short getSshort() { return sshort; } public void setSshort( short sshort) { this .sshort = sshort; } public double getSdouble() { return sdouble; } public void setSdouble( double sdouble) { this .sdouble = sdouble; } public String getSex() { return sex; } public void setSex(String sex) { this .sex = sex; } }
如果有任何好的建议请留言,共同探讨。

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

t150ckh

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值