Java中list、list、list>排序

1:list<Object[]>的排序

 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;

            }

        });

    }
 
Java中list<Object[]>、list<Student>、list<Map<String,String>>排序
 
 

2:list<Student>排序,主要是正对汉族的排序,按照拼音排序.

首先写一个Student类

    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;

        }

    }
然后在main方法中调用,(这里写的是一个java文件)
 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());

        }

写一个排序方法

 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;

            }

        });

    }
结果图:

Java中list<Object[]>、list<Student>、list<Map<String,String>>排序

 

3:list<Map<String,String>>排序问题:

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;
}
});
}
}
Java中list<Object[]>、list<Student>、list<Map<String,String>>排序
 
 
 
 
上一篇:C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法。


下一篇:wamp5.2 升级到wamp5.3 (转载)