场景:Java 类 需 重新排序,有时升序、有时倒序,有时是按照多字段排序
功能描述:
1.支持 对象的一个属性或多个属性进行排序.
2.支持 对象属性正序和倒序排列.
3.支持 数字字符类型的List排序
4.泛型实现,结合Comparable接口, 支持多类型数据格式
代码:
package GenericTest;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* [简述]:List通用排序工具类
* User: xyj
* Date:2016/7/2
* Time:10:24
* To change this template use File | Settings | File Templates.
*/
public class ListSortUtil {
private static final String SORT_ASC = "asc";
private static final String SORT_DESC = "desc";
/**
* [简述]: 对List数组排序
* @param list 源数据 排序集合
* @param sort 升序 还是 降序,默认升序
* @return List
*/
public static List> sort(List> list,final String sort){
Collections.sort(list, new Comparator
package utils.ListSortTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by with IntelliJ IDEA.
* User:xyj
* Date:2016/7/11
* Time:17:21
* To change this template use File | Settings | File Templates.
*/
@ContextConfiguration("classpath:spring/applicationContext.xml")
public class TestListSort extends AbstractTestNGSpringContextTests{
@Test
public void testListSortNumberString(){
List arrays = new ArrayList();
arrays.add("2");
arrays.add("13");
arrays.add("4.5");
arrays.add("26");
List afterSortArrays = (List) ListSortUtil.sort(arrays, "asc");
for(String s:afterSortArrays){
System.out.println(String.valueOf(s));
}
}
@Test
public void testListSortString(){
List arrays = new ArrayList();
arrays.add("ac");
arrays.add("cd");
arrays.add("ab");
arrays.add("bf");
arrays.add("bfd");
arrays.add("bg");
arrays.add("ba");
List afterSortArrays = (List) ListSortUtil.sort(arrays, "asc");
for(String s:afterSortArrays){
System.out.println(String.valueOf(s));
}
}
@Test
public void testListSortNumber(){
List arrays = new ArrayList();
arrays.add(12);
arrays.add(56);
arrays.add(2);
arrays.add(-5);
arrays.add(21);
arrays.add(0);
List afterSortArrays = (List) ListSortUtil.sort(arrays, "asc");
for(Integer s:afterSortArrays){
System.out.println(String.valueOf(s));
}
}
@Test
public void testListSortDouble() {
List arrays = new ArrayList();
arrays.add(-0.12);
arrays.add(-0.102);
arrays.add(12.0);
arrays.add(0.0);
arrays.add(9.2);
arrays.add(-1.2);
arrays.add(-1.20001);
arrays.add(3.0);
arrays.add(31.0);
List afterSortArrays = (List) ListSortUtil.sort(arrays, "asc");
for(Double s:afterSortArrays){
System.out.println(String.valueOf(s));
}
}
@Test
public void testListSortDate(){
List arrays = new ArrayList();
arrays.add(DateUtil.parseDate("2015-01-12",DateUtil.DATE_FORMAT_EN));
arrays.add(DateUtil.parseDate("2015-11-12",DateUtil.DATE_FORMAT_EN));
arrays.add(DateUtil.parseDate("2015-12-12",DateUtil.DATE_FORMAT_EN));
arrays.add(DateUtil.parseDate("2015-02-12",DateUtil.DATE_FORMAT_EN));
arrays.add(DateUtil.parseDate("2015-06-12",DateUtil.DATE_FORMAT_EN));
arrays.add(DateUtil.parseDate("2015-10-12",DateUtil.DATE_FORMAT_EN));
arrays.add(DateUtil.parseDate("2015-10-31",DateUtil.DATE_FORMAT_EN));
arrays.add(DateUtil.parseDate("2015-10-02",DateUtil.DATE_FORMAT_EN));
List afterSortArrays = (List) ListSortUtil.sort(arrays, "asc");
for(Date s:afterSortArrays){
System.out.println(String.valueOf(DateUtil.date2Str(s,DateUtil.DATE_FORMAT_EN)));
}
}
}
测试类 Two:
package utils.ListSortTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
/**
* Created by with IntelliJ IDEA.
* User:xyj
* Date:2016/7/11
* Time:14:48
* To change this template use File | Settings | File Templates.
*/
@ContextConfiguration("classpath:spring/applicationContext.xml")
public class TestListSortUtils extends AbstractTestNGSpringContextTests{
private static final Logger logger = LoggerFactory.getLogger(TestListSortUtils.class);
List sysUsers = new ArrayList();
private List getUsers(){
List sysUsers = new ArrayList();
SysUser sysUser3 = new SysUser();
sysUser3.setPkid("12");
sysUser3.setLoginName("naver");
sysUser3.setPassword("4");
sysUser3.setContact("15153");
sysUser3.setEnabled(2);
sysUsers.add(sysUser3);
SysUser sysUser2 = new SysUser();
sysUser2.setPkid("3");
sysUser2.setLoginName("Tex");
sysUser2.setPassword("3");
sysUser2.setContact("415151");
sysUser2.setEnabled(2);
sysUsers.add(sysUser2);
SysUser sysUser0 = new SysUser();
sysUser0.setPkid("1");
sysUser0.setLoginName("tommy");
sysUser0.setPassword("1");
sysUser0.setContact("123456");
sysUser0.setEnabled(1);
sysUsers.add(sysUser0);
SysUser sysUser1 = new SysUser();
sysUser1.setPkid("2");
sysUser1.setLoginName("tony");
sysUser1.setPassword("2");
sysUser1.setContact("5621232");
sysUser1.setEnabled(1);
sysUsers.add(sysUser1);
return sysUsers;
}
@BeforeClass
public void setInitBeforeSortList(){
sysUsers = getUsers();
logger.info("before sort :");
for(SysUser sysUser:sysUsers){
logger.info("sysUser: pkid: [ " + sysUser.getPkid() + " ]"
+ " userCode: [ "+ sysUser.getLoginName() + " ]"
+ " contact: [ "+ sysUser.getContact() + " ]"
);
}
}
@Test
public void testListSort(){
List afterSysUsers = ListSortUtil.sort(sysUsers,"loginName",null);
logger.info("after sort :");
for(SysUser sysUser:afterSysUsers){
logger.info("sysUser: pkid: [ " + sysUser.getPkid() + " ]"
+ " userCode: [ "+ sysUser.getLoginName() + " ]"
+ " contact: [ "+ sysUser.getContact() + " ]"
);
}
}
@AfterClass
public void destroy(){
logger.info("运行结束!");
}
}
测试 Three:
package utils.ListSortTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
/**
* Created by with IntelliJ IDEA.
* User:xyj
* Date:2016/7/11
* Time:14:48
* To change this template use File | Settings | File Templates.
*/
@ContextConfiguration("classpath:spring/applicationContext.xml")
public class TestListSortOrderUtils extends AbstractTestNGSpringContextTests{
private static final Logger logger = LoggerFactory.getLogger(TestListSortOrderUtils.class);
List sysUsers = new ArrayList();
private List getUsers(){
List sysUsers = new ArrayList();
SysUser sysUser3 = new SysUser();
sysUser3.setPkid("12");
sysUser3.setLoginName("naver");
sysUser3.setPassword("4");
sysUser3.setContact("15153");
sysUser3.setEnabled(2);
sysUsers.add(sysUser3);
SysUser sysUser2 = new SysUser();
sysUser2.setPkid("3");
sysUser2.setLoginName("Tex");
sysUser2.setPassword("3");
sysUser2.setContact("415151");
sysUser2.setEnabled(2);
sysUsers.add(sysUser2);
SysUser sysUser0 = new SysUser();
sysUser0.setPkid("1");
sysUser0.setLoginName("tommy");
sysUser0.setPassword("1");
sysUser0.setContact("123456");
sysUser0.setEnabled(1);
sysUsers.add(sysUser0);
SysUser sysUser1 = new SysUser();
sysUser1.setPkid("2");
sysUser1.setLoginName("tony");
sysUser1.setPassword("2");
sysUser1.setContact("5621232");
sysUser1.setEnabled(1);
sysUsers.add(sysUser1);
return sysUsers;
}
@BeforeClass
public void setInitBeforeSortList(){
sysUsers = getUsers();
logger.info("before sort :");
for(SysUser sysUser:sysUsers){
logger.info("sysUser: pkid: [ " + sysUser.getPkid() + " ]"
+ " userCode: [ "+ sysUser.getLoginName() + " ]"
+ " contact: [ "+ sysUser.getContact() + " ]"
);
}
}
@Test
public void testListSort(){
List afterSysUsers = ListSortUtil.sort(sysUsers,new String[] {"pkid","contact"},new String[] {"asc","desc"});
logger.info("after sort :");
for(SysUser sysUser:afterSysUsers){
logger.info("sysUser: pkid: [ " + sysUser.getPkid() + " ]"
+ " userCode: [ "+ sysUser.getLoginName() + " ]"
+ " contact: [ "+ sysUser.getContact() + " ]"
);
}
}
@AfterClass
public void destroy(){
logger.info("运行结束!");
}
}