本文目录
- Java 如何根据List
中TblTrad中的字段排序 - java list 相同的字段排序
- java根据某列排序问题
- java 中 List
如何按照T中的一个字段排序 - java如何让list按照list里面的某个字段排序,list里面的有很多字段!
- java中一个List集合,放的都是从不同的表中查出来的数据,请问我怎样可以根据其中的一个字段进行list排序
Java 如何根据List中TblTrad中的字段排序
Collections.sort(List《T》 list, Comparator《? super T》 c)或者让你的TblTrad实现Comparable接口,用Collections.sort(List《T》 list)
java list 相同的字段排序
给你个思路吧,难到不难,不过做出来,没什么效率可言。纯粹是练手罢了。首先,遍历你的集合list1,找出ID字段值相等的对象,并将相等的字段值存入一个新的集合list2中。新集合就是你要的所有名字相同的字段值。嵌套for循环,遍历list2找出list1中ID值与其值相等的所有对象并保存到新集合list3中。name字段的排序同上。说起来太麻烦了。你好好想想把。
java根据某列排序问题
可以使用Collections集合工具类对它进行排序
public class Test {public static void main(String args){List《Student》 students=new ArrayList《Test.Student》();StudentComparator comparator = new StudentComparator();//比较语文comparator.setCompareType(StudentComparator.COMPARE_CHINESE);Collections.sort(students, comparator);//TODO 此时students中的对象已经按语文成绩排好序,此时可以对结果进行操作//比较数学comparator.setCompareType(StudentComparator.COMPARE_MATH);Collections.sort(students, comparator);//TODO comparator.setCompareType(StudentComparator.COMPARE_ENGLISH);Collections.sort(students, comparator);//TODO 此时students中的对象已经按语文成绩排好序,此时可以对结果进行操作}static class Student{//各科成绩 private int chinese,math,english;public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getEnglish() {return english;}public void setEnglish(int english) {this.english = english;}}}
这个类是需要实现的比较类
class StudentComparator implements Comparator《Student》 {public static final int COMPARE_CHINESE = 0, COMPARE_MATH = 1,COMPARE_ENGLISH = 2;// 比较的科目类型int compareType;public StudentComparator(){this.compareType=COMPARE_CHINESE;}public StudentComparator(int compareType) {this.compareType = compareType;}//比较两个对象中特定域的值,返回值大于0说明o1》o2,小于0说明o1《o2,等于0说明o1=o2@Overridepublic int compare(Student o1, Student o2) {switch (this.compareType) {case COMPARE_CHINESE: return o1.getChinese()-o2.getChinese();case COMPARE_MATH: return o1.getMath()-o2.getMath();case COMPARE_ENGLISH: return o1.getEnglish()-o2.getEnglish(); default:throw new RuntimeException("invalid compare type");}}public int getCompareType() {return compareType;}public void setCompareType(int compareType) {this.compareType = compareType;}}
java 中 List如何按照T中的一个字段排序
可以通过以下工具类进行实现:import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;/** * List按照指定字段排序工具类 * * @param 《T》 */public class ListSortUtil《T》 { /** * @param targetList 目标排序List * @param sortField 排序字段(实体类属性名) * @param sortMode 排序方式(asc or desc) */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void sort(List《T》 targetList, final String sortField, final String sortMode) { Collections.sort(targetList, new Comparator() { public int compare(Object obj1, Object obj2) { int retVal = 0; try { //首字母转大写 String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w",""); String methodStr="get"+newStr; Method method1 = ((T)obj1).getClass().getMethod(methodStr, null); Method method2 = ((T)obj2).getClass().getMethod(methodStr, null); if (sortMode != null && "desc".equals(sortMode)) { retVal = method2.invoke(((T) obj2), null).toString().compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序 } else { retVal = method1.invoke(((T) obj1), null).toString().compareTo(method2.invoke(((T) obj2), null).toString()); // 正序 } } catch (Exception e) { throw new RuntimeException(); } return retVal; } }); } } Collections.sort(list.);//升序
java如何让list按照list里面的某个字段排序,list里面的有很多字段!
给你个例子看下
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;public class $ { public static void main(String args) { List《Map《String, Integer》》 data = new ArrayList《Map《String, Integer》》(); init(data); System.out.println("排序前:"); System.out.println(data); sort(data); System.out.println("排序后:"); System.out.println(data); } private static void sort(List《Map《String, Integer》》 data) { Collections.sort(data, new Comparator《Map》() { public int compare(Map o1, Map o2) { Integer a = (Integer) o1.get("PRECOUNTOUT"); Integer b = (Integer) o2.get("PRECOUNTOUT"); // 升序 return a.compareTo(b); // 降序 // return b.compareTo(a); } }); } private static void init(List《Map《String, Integer》》 data) { Map《String, Integer》 map = new HashMap《String, Integer》(); map.put("COUNTTICKET", 1); map.put("PRECOUNTOUT", 2); data.add(map); map = new HashMap《String, Integer》(); map.put("COUNTTICKET", 6); map.put("PRECOUNTOUT", 7); data.add(map); map = new HashMap《String, Integer》(); map.put("COUNTTICKET", 8); map.put("PRECOUNTOUT", 5); data.add(map); map = new HashMap《String, Integer》(); map.put("COUNTTICKET", 2); map.put("PRECOUNTOUT", 3); data.add(map); }}
java中一个List集合,放的都是从不同的表中查出来的数据,请问我怎样可以根据其中的一个字段进行list排序
用 Collections.sort()排序,给你个例子,你参考下
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class $ { public static void main(String args) { List《Bean》 data = new ArrayList《Bean》(); data.add(new Bean("aaaaa", 20)); data.add(new Bean("bbbbb", 10)); System.out.println("排序前:" + data); Collections.sort(data, new Comparator《Bean》() { public int compare(Bean o1, Bean o2) { return o1.getName().compareTo(o2.getName()); } }); System.out.println("按name升序:" + data); Collections.sort(data, new Comparator《Bean》() { public int compare(Bean o1, Bean o2) { return o2.getName().compareTo(o1.getName()); } }); System.out.println("按name降序:" + data); Collections.sort(data, new Comparator《Bean》() { public int compare(Bean o1, Bean o2) { return o1.getAge() - o2.getAge(); } }); System.out.println("按age升序:" + data); Collections.sort(data, new Comparator《Bean》() { public int compare(Bean o1, Bean o2) { return o2.getAge() - o1.getAge(); } }); System.out.println("按age降序:" + data); }}class Bean { private String name; private int age; public Bean(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return ""; }}