java 对象数组排序

java对象List集合的排序(基本类型相似);

方法一:使用for循环的方式来进行对象List集合的排序;
方法二:使用内部比较器(被比较的对象实现comparable接口,并重写compareTo()方法);
方法三:使用外部比较器(使用Collections.sort(要排序的list,外部比较器)或者Arrays.sort(要排序的数组,外部比较器)方法);

注意:
1.Collections.sort()主要是对list集合进行排序;Arrays.sort()主要是对各类型的array数组进行排序;
2.内部比较器、外部比较器同时使用的时候,只有外部比较器生效;

package com.arraySort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Person implements Comparable {

    private String name;

    private int score;

    public Person(int score, String name) {
        super();
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public int compareTo(Person o) {
//        从小到大排序
        return this.score - o.getScore();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    private static List initList() {
        List persons = new ArrayList();
        persons.add(new Person(980, "M1"));
        persons.add(new Person(34, "M2"));
        persons.add(new Person(12, "M3"));
        persons.add(new Person(65, "M4"));
        persons.add(new Person(12, "M5"));
        persons.add(new Person(33, "M6"));
        persons.add(new Person(90, "M7"));
        persons.add(new Person(123, "M8"));
        return persons;
    }

    private static void pirntList(String title,List persons){
        System.out.println("=============="+title+"=================");
        for (Person person : persons) {
            System.out.println(person.toString());
        }
    }

    public static void main(String[] args) {

//        直接使用for循环,不建议使用,重复造轮子了
        List persons = initList();
        Person[] personArray = persons.toArray(new Person[persons.size()]);
//                从大到小排序
        for (int i = 0; i < personArray.length; i++) {
            for (int j = i + 1; j < personArray.length; j++) {
                if (personArray[j].getScore() > personArray[i].getScore()) {
                    Person tmp = personArray[i];
                    personArray[i] = personArray[j];
                    personArray[j] = tmp;
                }

            }
        }
        persons = Arrays.asList(personArray);
        pirntList("直接使用for循环,从大到小",persons);

//        从小到大排序
        persons = initList();
        personArray = persons.toArray(new Person[persons.size()]);
        for (int i = personArray.length - 1; i >= 0; i--) {
            for (int j = i - 1; j >= 0; j--) {
                if (personArray[j].getScore() > personArray[i].getScore()) {
                    Person tmp = personArray[i];
                    personArray[i] = personArray[j];
                    personArray[j] = tmp;
                }

            }
        }
        persons = Arrays.asList(personArray);
        pirntList("直接使用for循环,从小到大",persons);


//        内部比较器,排序方式主要由比较器决定
        persons = initList();
//        Collections.sort(persons);
        pirntList("内部比较器排序,从小到大",persons);


//        外部比较器,注意:当内部比较器和外部比较器同时使用的时候,只有外部比较器生效;
        persons = initList();
        Collections.sort(persons, (o1, o2) -> {
//                从小到大排序
//                return o1.getScore()-o2.getScore();
//                从大到小排序;
            return o2.getScore()-o1.getScore();
        });
        //        lambda写法,reversed()表示降序排序,不添加默认使用升序排序
//        persons=persons.stream().sorted(Comparator.comparing(Person::getScore).reversed()).collect(Collectors.toList());
//        persons=persons.stream().sorted(Comparator.comparing(Person::getScore)).collect(Collectors.toList());
        pirntList("外部比较器排序,从大到小",persons);

    }
}

执行结果如下:

==============直接使用for循环,从大到小=================
Person{name='M1', score=980}
Person{name='M8', score=123}
Person{name='M7', score=90}
Person{name='M4', score=65}
Person{name='M2', score=34}
Person{name='M6', score=33}
Person{name='M5', score=12}
Person{name='M3', score=12}
==============直接使用for循环,从小到大=================
Person{name='M3', score=12}
Person{name='M5', score=12}
Person{name='M6', score=33}
Person{name='M2', score=34}
Person{name='M4', score=65}
Person{name='M7', score=90}
Person{name='M8', score=123}
Person{name='M1', score=980}
==============内部比较器排序,从小到大=================
Person{name='M3', score=12}
Person{name='M5', score=12}
Person{name='M6', score=33}
Person{name='M2', score=34}
Person{name='M4', score=65}
Person{name='M7', score=90}
Person{name='M8', score=123}
Person{name='M1', score=980}
==============外部比较器排序,从大到小=================
Person{name='M1', score=980}
Person{name='M8', score=123}
Person{name='M7', score=90}
Person{name='M4', score=65}
Person{name='M2', score=34}
Person{name='M6', score=33}
Person{name='M3', score=12}
Person{name='M5', score=12}

你可能感兴趣的:(java 对象数组排序)