void sortList(List<DoublePubAddEntity> doublePubSummaryEntities, String sortname, String sortorder){ if(!StringUtils.isEmpty(sortname) && !StringUtils.isEmpty(sortorder) && doublePubSummaryEntities!=null){ for(int i=0;i<doublePubSummaryEntities.size();i++){ for(int j=i+1;j<doublePubSummaryEntities.size();j++){ DoublePubAddEntity tempi = doublePubSummaryEntities.get(i); DoublePubAddEntity tempj = doublePubSummaryEntities.get(j); try { Field faFieldi = tempi.getClass().getDeclaredField(sortname); Field faFieldj = tempj.getClass().getDeclaredField(sortname); if(faFieldi!=null && faFieldj!=null) { faFieldi.setAccessible(true); //设置些属性是可以访问的 faFieldj.setAccessible(true); Object vali = faFieldi.get(tempi);//得到此属性的值 Object valj = faFieldj.get(tempj);//得到此属性的值 if (vali != null && valj != null) { int vi = 0; int vj = 0; if ("--".equals(vali) ) { vi = 0; }else{ String typei = faFieldi.getType().toString();//得到此属性的类型 if (typei.endsWith("String") ) { vi = Integer.valueOf(vali.toString()); } } if( "--".equals(valj)){ vj = 0; }else{ String typej = faFieldj.getType().toString();//得到此属性的类型 if (typej.endsWith("String") ) { vj = Integer.valueOf(valj.toString()); } } if (sortorder.toLowerCase().equals("asc")) { if (vi > vj) { doublePubSummaryEntities.set(i, tempj); doublePubSummaryEntities.set(j, tempi); } } else if (sortorder.toLowerCase().equals("desc")) { if (vi < vj) { doublePubSummaryEntities.set(i, tempj); doublePubSummaryEntities.set(j, tempi); } } } } } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } }
java对实体类列表进行方法内排序,不修改数据查询方式
最新推荐文章于 2024-01-16 10:11:48 发布