[Java] 使用Comparator排序对象

package test.collections;

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

public class Test03 {
    public static void main(String[] args){
        List<Song> songs = new ArrayList<Song>();
        songs.add(new Song(2,"黄家驹","光辉岁月"));
        songs.add(new Song(5,"刘德华","忘情水"));
        songs.add(new Song(44,"张学友","吻别"));
        songs.add(new Song(32,"刘德华","爱你一万年"));
        songs.add(new Song(123,"黄家驹","冷雨夜"));
        songs.add(new Song(133,"小虎队","爱"));

        //Collections.sort(songs);
        //Collections.sort(songs,new SongNameCompare());
        //Collections.sort(songs,new SongSongerCompare());
        Collections.sort(songs,new SongSongIdCompare());
        printList(songs);
    }
    public static void printList(Collection<Song> c){
        for(Song song:c){
            System.out.println(song);
        }
    }
}
class SongNameCompare implements Comparator<Song>{

    @Override
    public int compare(Song o1, Song o2) {
        // TODO Auto-generated method stub
        return o1.getName().compareTo(o2.getName());
    }
}
class SongSongerCompare implements Comparator<Song>{
    @Override
    public int compare(Song o1, Song o2) {
        // TODO Auto-generated method stub
        return o1.getSonger().compareTo(o2.getSonger());
    }
}
class SongSongIdCompare implements Comparator<Song>{
    @Override
    public int compare(Song o1, Song o2) {
        // TODO Auto-generated method stub
        return o1.id>o2.id?1:(o1.id<o2.id?-1:0);
    }
}
class Song implements Comparable<Song>{
    private String name;
    private String songer;
    int id;
    public Song(int id,String songer,String name) {
        super();
        this.id = id;
        this.name = name;
        this.songer = songer;
    }

    @Override
    public String toString() {
        return "Song [name=" + name + ", songer=" + songer + ", id=" + id + "]";
    }

    public Song() {
        super();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSonger() {
        return songer;
    }
    public void setSonger(String songer) {
        this.songer = songer;
    }
    public int compareTo(Song s){
        //使用this.id 和s.id做比较,如果返回值>1,表示顺序排序,返回值是<1的数表示倒序
        if(this.id>s.id){
            return 1;
        }else if(this.id<s.id){
            return -1;
        }else {
            return 0;
        }

        //根据歌名排序
        //return this.getName().compareTo(s.getName());
        //根据歌手名排序
        //return this.getSonger().compareTo(s.getSonger());
    }
}
时间: 2024-10-13 06:21:07

[Java] 使用Comparator排序对象的相关文章

Java中Comparator进行对象排序

Java在8后引入了lambda表达式和流,使得排序方法有了变化 class User { int id; String name; public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } @Override public String toString() { re

[转] Java中Comparator进行对象排序

[From] https://blog.51cto.com/thinklili/2063244 Java在8后引入了lambda表达式和流,使得排序方法有了变化 class User { int id; String name; public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return

实现java.util.Comparator接口,对对象集合进行多属性组合排序

Commons - BeanUtils 提供了很多功能,其中一个很有用的是对对象集合进行排序,如Collections.sort(peoples, new BeanComparator("age")); 另外,可以使用java.util.Collections的sort方法可以对collection集合进行排序,包括多列组合排序,下面是自己实现java.util.Comparator,定制对象属性排序规则的例子: package com.lph.test; import java.ut

Java中Comparable和Comparator实现对象比较

1.通过Comparable实现排序 package Comparable; import java.util.Arrays; public class ComparableUser implements Comparable<ComparableUser> { private String id; private int age; public ComparableUser(String id, int age) { this.id = id; this.age = age; } publi

Comparable与Comparator,java中的排序与比较

1:比较和排序的概念 比较:两个实体类之间按>,=,<进行比较. 排序:在集合类中,对集合类中的实体进行排序.排序基于的算法基于实体类提供的比较函数. 基本型别都提供了默认的比较算法,如string提供了按字母进行比较,int提供了按整数大小进行比较. 2:Comparable与Comparator but,在软件开发的世界中,任何没有代码的堆概念都是耍流氓.所以,无论我们解释的多么完美,必须show me the code: 我们首先看这样一段代码: public class Collect

使用Java中Comparator接口实现自定义排序

一般情况下,自己动手写一个简单排序程序还是没有问题的,但是你能保证写出来的排序程序的时间复杂度吗?你能保证程序的正确性吗,鲁棒性呢,还有程序结构的清晰性,可维护性.......综上所述,学习一下排序接口来实现对复杂对象的排序还是很有必要的.Java中有两个用来实现排序的接口Comparator和Comparable接口,本人比较喜欢使用java的Comparator接口,在程序里实现Comparator接口里的compare(Object o1,Object o2)方法,然后在程序中通过调用Ar

java中的排序Comparable接口和Comparator接口

普通的类要实现排序,必须实现Comparable接口,并重写CompareTo()方法. package test; public class Field implements Comparable<Field> {     private String name;     private int age;     public Field() {     }     public Field(String name, int age) {         this.name = name;

java 对list中对象按属性排序

实体对象类 --略 排序类----实现Comparator接口,重写compare方法 package com.tang.list; import java.util.Comparator; public class Mycompera implements Comparator<Student> { @Override    public int compare(Student o1, Student o2) {        if (o1.getSid() > o2.getSid()

java ArrayList根据存储对象排序

与c++中的qsort的实现极为相似,构建新的比较对象Comparator即可 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class Student{ public int age; private String name; public Student(String name,int age){ this.age=age; this.name=name; } pu