根据比较器使用字符串日期排序?

时间:2015-12-10 10:48:58

标签: java date comparator

我有以下课程:

public class Book implements Cloneable {
    private int ISBN;
    private String title;
    private String publishDate;
    private int pageCount;
}

我可以使用以下代码依赖于Comparator进行排序:

Comparator<Book> titleComparator = new Comparator<Book>() {
    @Override
    public int compare(Book o1, Book o2) {
        return o1.title.compareTo(o2.title);
    }
};

Comparator<Book> ISBNComparator = new Comparator<Book>() {
    @Override
    public int compare(Book o1, Book o2) {
        return Integer.valueOf(o1.ISBN).compareTo(o2.ISBN);
    }
};

public void sort(final String field, ArrayList<Book> BookList) {
    final Comparator<Book> comparator;

    if (field.equals("title")) {
        comparator = titleComparator;
    } else if (field.equals("ISBN")) {
        comparator = ISBNComparator;
    } else {
        throw new IllegalArgumentException("Comparator not found for " + field);
    }

    Collections.sort(BookList, comparator);
}

但是当发布日期格式为“2015年12月25日”时,如何根据String publishDate进行排序?

3 个答案:

答案 0 :(得分:3)

如果您确定使用Date而不是String,则使用此比较器(您可以添加一些缓存以避免每次都解析字符串):

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;

/**
 *
 * @author EBudaqov
 */
public class StringDateComparator implements Comparator<String>{

    private SimpleDateFormat formatter;

    public StringDateComparator(String dateFormat) {
        formatter = new SimpleDateFormat(dateFormat);
    }

    @Override
    public int compare(String s1, String s2) {
        try {
            return formatter.parse(s1).compareTo(formatter.parse(s2));
        } catch(ParseException exception) {
            throw new RuntimeException(exception);
        }
    }

    public static void main(String[] args) {
        String s1 = "01/01/2015";
        String s2 = "02/01/2015";

        System.out.println(new StringDateComparator("dd/MM/yyyy").compare(s1, s2));
    }

}

答案 1 :(得分:1)

您需要以java.util.Date()格式解析日期,然后进行比较。请尝试以下示例代码:

Comparator<Book> dateComparator = new Comparator<Book>() {
    @Override
    public int compare(Book o1, Book o2) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date o1PublishDate = null;
        Date o2PublishDate = null;
        try {
            o1PublishDate = formatter.parse(o1.publishDate);
            o2PublishDate = formatter.parse(o2.publishDate);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }

        return o1PublishDate.compareTo(o2PublishDate);

    }
};

答案 2 :(得分:1)

如果你正在使用Java7或尝试使用新的Java 8 Date API,我认为你的publishDate应该是java.util.Date对象。但是,代码应该是这样的:

Comparator<Book> titleComparator = new Comparator<Book>() {
    @Override
    public int compare(Book o1, Book o2) {
        return o1.title.compareTo(o2.title);
    }
};
Comparator<Book> ISBNComparator = new Comparator<Book>() {

    @Override
    public int compare(Book o1, Book o2) {
        return Integer.valueOf(o1.ISBN).compareTo(o2.ISBN);
    }
};
Comparator<Book> PublishComparator = new Comparator<Book>() {

    DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    @Override
    public int compare(Book o1, Book o2) {
        Date d1 = format.parse(o1.publishDate);
        Date d2 = format.parse(o2.publishDate);

        return d1.compareTo(d2);
    }
};
public void sort(final String field, ArrayList<Book> BookList) {

    final Comparator<Book> comparator;

    if (field.equals("title")) {
        comparator = titleComparator;
    } else if (field.equals("ISBN")) {
        comparator = ISBNComparator;
    } else if (field.equals("publishDate")) {
        comparator = PublishComparator;
    } else {
        throw new IllegalArgumentException("Comparator not found for " + field);
    }
    Collections.sort(BookList, comparator);
}