在工作中,经常需要对List对象集合进行排序操作,下面总结下搞个通用排序对象,原理是使用JAVA的
Comparator 接口实现排序 不多说直接上“干货”
1、存在实体类:
1 1 @Data 2 2 @AllArgsConstructor 3 3 @NoArgsConstructor 4 4 class Book { 5 5 private Long id;//编号 6 6 private String userName;//书本名称 7 7 private double productPrice;//书本价格 8 8 private String author;//作者 9 9 private Integer weight;//权重 1010 }
2、SortList 排序实现通用类:
1 1 @Data 2 2 @AllArgsConstructor 3 3 @NoArgsConstructor 4 4 public class SortList<T> implements Comparator<T> { 5 5 6 6 //需要比较的对象属性字段名称 7 7 private String propertyName; 8 8 //是否是升序排序 9 9 private boolean isAsc; 1010 1111 /** 1212 * 需要的是:根据类中的字段对对象进行排序 1313 * 1414 * @return 1515 */ 1616 1717 @Override 1818 public int compare(T b1, T b2) { 1919 2020 Class<?> clz = b1.getClass(); 2121 Method method = getPropertyMethod(clz, propertyName); 2222 try { 2323 2424 Object objectOne = method.invoke(b1); 2525 2626 Object objectTwo = method.invoke(b2); 2727 2828 if (objectOne == null || objectTwo == null) { 2929 return 0; 3030 } 3131 3232 Comparable value1 = (Comparable) objectOne; 3333 3434 Comparable value2 = (Comparable) objectTwo; 3535 3636 if (isAsc) { 3737 return value1.compareTo(value2); 3838 } else { 3939 return value2.compareTo(value1); 4040 } 4141 } catch (Exception e) { 4242 e.printStackTrace(); 4343 } 4444 return 0; 4545 } 4646 4747 // 获取类名 4848 public static Method getPropertyMethod(Class clz, String propertyName) { 4949 Method method = null; 5050 try { 5151 method = clz.getMethod("get" + firstUpperCase(propertyName)); 5252 } catch (Exception e) { 5353 System.out.println("获取类名发生错误!"); 5454 } 5555 return method; 5656 } 5757 5858 /** 5959 * 首字母大写方法 6060 * @param str 6161 * @return 6262 */ 6363 public static String firstUpperCase(String str) { 6464 char[] ch = str.toCharArray(); 6565 if (ch[0] >= 'a' && ch[0] <= 'z') { 6666 ch[0] = (char) (ch[0] - 32); 6767 } 6868 return new String(ch); 6969 } 7070 7171 }
3、实际使用测试如下: 主要这么来使用
Collections.sort(bookList, new SortList<Book>("productPrice",true));
测试
1 1 @Test 2 2 public void sortBook() { 3 3 List<Book> bookList = getBookList(); 4 4 System.out.println("原先的顺序:"); 5 5 printf(bookList); 6 6 7 7 System.out.println("根据价格排序:"); 8 8 Collections.sort(bookList, new SortList<Book>("productPrice",true)); 9 9 printf(bookList); 1010 1111 System.out.println("根据Id排序:"); 1212 Collections.sort(bookList, new SortList<Book>("id",false)); 1313 printf(bookList); 1414 1515 System.out.println("根据weight排序:"); 1616 Collections.sort(bookList, new SortList<Book>("weight",true)); 1717 printf(bookList); 1818 1919 System.out.println("根据userName排序:"); 2020 Collections.sort(bookList, new SortList<Book>("userName",true)); 2121 printf(bookList); 2222 2323 2424 } 2525 2626 public List<Book> getBookList() { 2727 List<Book> books = Lists.newArrayList(); 2828 Book book1 = new Book(1L, "first", 10.00, "zhangsan", 19); 2929 Book book2 = new Book(2L, "wirst", 9.00, "zhangsan", 24); 3030 Book book3 = new Book(3L, "eirst", 8.00, "zhangsan", 29); 3131 Book book4 = new Book(4L, "girst", 7.00, "zhangsan", 13); 3232 Book book5 = new Book(5L, "tirst", 6.00, "zhangsan", 14); 3333 3434 books.add(book1); 3535 books.add(book2); 3636 books.add(book3); 3737 books.add(book4); 3838 books.add(book5); 3939 4040 return books; 4141 } 4242 4343 /** 4444 * 打印函数 4545 * 4646 * @param lisbk 4747 */ 4848 public void printf(List<Book> lisbk) { 4949 if (lisbk.isEmpty() || lisbk == null) { 5050 System.out.println("没有数据"); 5151 return; 5252 } 5353 for (Book book : lisbk) { 5454 System.out.println("Id: " + book.getId() + " userName: " + book.getUserName() + " price: " + book.getProductPrice() + " weight:" + book.getWeight()); 5555 } 5656 System.out.println(); 5757 return; 5858 }
执行结果如下:
