码迷,mamicode.com
首页 > 编程语言 > 详细

Java 实现 对象List 进行排序

时间:2020-01-12 00:32:58      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:setname   style   creat   over   运行   ble   ati   排名   lun   

Java 实现 对象List 进行排序


按照对象中的某个属性,对对象List进行排序。

以初唐四杰的成绩排名为例,对诗人进行排序。

 

Java实现如下:

1、诗人(Poet)类结构,定义如下:

/**
 * Created by Miracle Luna on 2020/1/11
 */
public class Poet {
    private String name;
    private Double score;

    public Poet(String name, Double score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

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

    public Double getScore() {
        return score;
    }

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

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

 

2、诗人按照成绩排名,代码如下:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Miracle Luna on 2020/1/11
 */
public class PoetSort {

    public static void main(String[] args) {
        List<Poet> poetList = new ArrayList<Poet>();
        Poet poet1 = new Poet("杨炯", 94.0);
        poetList.add(poet1);
        Poet poet2 = new Poet("卢照邻", 92.5);
        poetList.add(poet2);
        Poet poet3 = new Poet("骆宾王", 95.0);
        poetList.add(poet3);
        Poet poet4 = new Poet("王勃", 99.5);
        poetList.add(poet4);


        // 初始顺序
        System.out.println("==> 初始顺序如下:");
        poetList.forEach(poet -> System.out.println(poet.toString()));

        // 按照分数排名(从高到低)
        poetList.sort(new Comparator<Poet>() {
            @Override
            public int compare(Poet poet1, Poet poet2) {
                Double score1 = poet1.getScore();
                Double score2 = poet2.getScore();
                return score2.compareTo(score1);
            }
        });
        System.out.println("\n==> 按照分数排名(从高到低)如下:");
        poetList.forEach(poet -> System.out.println(poet.toString()));

        // 按照分数排名(从低到高)
        poetList.sort(new Comparator<Poet>() {
            @Override
            public int compare(Poet poet1, Poet poet2) {
                Double score1 = poet1.getScore();
                Double score2 = poet2.getScore();
                return score1.compareTo(score2);
            }
        });
        System.out.println("\n==> 按照分数排名(从低到高)如下:");
        poetList.forEach(poet -> System.out.println(poet.toString()));
    }
}

 

3、运行结果如下:

==> 初始顺序如下:
Poet{name=‘杨炯‘, score=94.0}
Poet{name=‘卢照邻‘, score=92.5}
Poet{name=‘骆宾王‘, score=95.0}
Poet{name=‘王勃‘, score=99.5}

==> 按照分数排名(从高到低)如下:
Poet{name=‘王勃‘, score=99.5}
Poet{name=‘骆宾王‘, score=95.0}
Poet{name=‘杨炯‘, score=94.0}
Poet{name=‘卢照邻‘, score=92.5}

==> 按照分数排名(从低到高)如下:
Poet{name=‘卢照邻‘, score=92.5}
Poet{name=‘杨炯‘, score=94.0}
Poet{name=‘骆宾王‘, score=95.0}
Poet{name=‘王勃‘, score=99.5}

 

 

Java 实现 对象List 进行排序

标签:setname   style   creat   over   运行   ble   ati   排名   lun   

原文地址:https://www.cnblogs.com/miracle-luna/p/12181535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!