public static void main(String[] args) { // TODO Auto-generated method stub Object[] o1 = new Object[4]; o1[0] = "5"; o1[1] = "o1"; o1[2] = "o11"; o1[3] = "o111"; Object[] o2 = new Object[4]; o2[0] = "3"; o2[1] = "o2"; o2[2] = "o22"; o2[3] = "o222"; List<Object[]> list = new ArrayList<>(); list.add(o1); list.add(o2); //很明顯咱們先添加的對象o1,因此先打印o1, for (inti = 0; i < list.size(); i++) { for (intj = 0; j < 4; j++) { System.out.print(list.get(i)[j] + " "); } } System.out.println("\n排序後-------"); sortList(list); //排序後: for (inti = 0; i < list.size(); i++) { for (intj = 0; j < 4; j++) { System.out.print(list.get(i)[j] + " "); } } }
public staticvoid sortList(List<Object[]> ls) { Collections.sort(ls, new Comparator<Object[]>() { @Override public int compare(Object[] o1, Object[] o2) { if (Integer.valueOf(o1[0].toString()) > Integer.valueOf(o2[0].toString())) { return 1; } return -1; } }); }
package com.model; public class Student { public Stringname; publicintage; public Student(Stringname, intage) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { returnname; } public void setAge(int age) { this.age = age; } public int getAge() { returnage; } }
Student stu1 = new Student("張三", 23); Student stu2 = new Student("李四", 25); List<Student> listStudent = new ArrayList<>(); listStudent.add(stu1); listStudent.add(stu2); System.out.println(); for (int i = 0; i < listStudent.size(); i++) { System.out.print(listStudent.get(i).getName() + "---" + listStudent.get(i).getAge()); } System.out.println("\n排序後"); sortListStudent(listStudent); for (int i = 0; i < listStudent.size(); i++) { System.out.print(listStudent.get(i).getName() + "---" + listStudent.get(i).getAge()); }
寫一個排序方法java
public staticvoid sortListStudent(List<Student> ls) { Collections.sort(ls, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { String s1 = o1.getName(); String s2 = o2.getName(); if (s1.compareTo(s2) > 0) { return -1; } return 1; } }); }
public class MapListSort { public static void main(String[] args) { // TODO Auto-generated method stub List<Map<String, String>> listMap = new ArrayList<>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("map001", "001"); map1.put("map003", "map003"); map1.put("map002", "map002"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("map001", "101"); map2.put("map003", "map303"); map2.put("map002", "map202"); //先添加的map2,可是map2中map001的值大於map1中的map001的值。 listMap.add(map2); listMap.add(map1); for (int i = 0; i < listMap.size(); i++) { System.out.print(listMap.get(i).get("map001") + "," + listMap.get(i).get("map002") + "," + listMap.get(i).get("map003") + "\n"); } mapSorts(listMap); System.out.println("\n排序後:"); for (int i = 0; i < listMap.size(); i++) { System.out.print(listMap.get(i).get("map001") + "," + listMap.get(i).get("map002") + "," + listMap.get(i).get("map003") + "\n"); } } public static void mapSorts(List<Map<String, String>> map) { Collections.sort(map, new Comparator<Map<String, String>>() { @Override public int compare(Map<String, String> o1, Map<String, String> o2) { // TODO Auto-generated method stub if (o1.get("map001").compareTo(o2.get("map001")) > 0) { return 1; } return -1; } }); } }