Java中的对象排序

当我们要对集合中的对象进行排序是,需要使用Comparable&Comparator,从而实现对象的排序。

一、使用Comparable接口完成排序:实现此接口的对象列表可以通过Collection.sort进行自动排序。

package com.wjl.study.sort.object;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.alibaba.fastjson.JSON;

public class Student implements Comparable<Student> {
    private String name;
    private int age;
    private String number;

    public Student() {

    }

    public Student(String name, int age, String number) {
        this.name = name;
        this.age = age;
        this.number = number;
    }

    @Override
    public int compareTo(Student stu) {
        return number.compareTo(stu.number);
    }

    public String toString() {
        //使用json进行数据拼装,需要进行fastjson jar包
        return JSON.toJSONString(this);
    }

    // 通过学号进行排序
    public static void main(String[] args) {
        //
        final int flag = -1;
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(new Student("张三", 30, "200906084130"));
        list.add(new Student("李四", 24, "200906084111"));
        list.add(new Student("王五", 66, "200906084166"));
        list.add(new Student("赵六", 34, "200906084132"));
        System.out.println("before sort: " + list);
        Collections.sort(list, new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                //降序
                return o1.compareTo(o2) * flag;
            }

        });
        System.out.println("after sort1: " + list);
        //升序
        Collections.sort(list);
        System.out.println("after sort2: " + list);
    }

    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 getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

二、使用Comparator接口实现排序:实现compare方法,通过比较参数大小的返回值来进行对象排序。

package com.wjl.study.sort.objectsort;

public class Student {
    private String name;
    private int age;
    private String number;

    public Student(String name, int age, String number) {
        this.name = name;
        this.age = age;
        this.number = number;
    }

    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 getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

package com.wjl.study.sort.objectsort;

import java.util.Comparator;

/**
 * 使用Comparator接口实现排序
 * 
 * @author weijunlong
 * 
 */
public class MyCompare implements Comparator<Student> {

    private int flag = 1;

    public MyCompare() {

    }

    public MyCompare(int flag) {
        this.flag = flag;
    }

    @Override
    public int compare(Student stu1, Student stu2) {
        return stu1.getNumber().compareTo(stu2.getNumber()) * flag;
    }
}

package com.wjl.study.sort.objectsort;

import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static void main(String args[]) {
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(new Student("张三", 30, "200906084130"));
        list.add(new Student("李四", 24, "200906084111"));
        list.add(new Student("王五", 66, "200906084166"));
        list.add(new Student("赵六", 34, "200906084132"));
        System.out.println("before sort: " + list);
        Collections.sort(list, new MyCompare(1));
        System.out.println("after sort: " + list);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值