java - 使用 compareTo 对对象进行排序(多种类型)

标签 java compareto

首先,我有一个名为 news article 的对象,它具有三个我需要排序的属性:

年(int),月(int),类型(String - online, paper)

例如,它会是这样的:

  • 在线 2013 4
  • 在线 2013 1
  • 在线 2009 年 11
  • 在线 2008 4
  • 论文 2012 12
  • 论文 2011 9

发生的事情是月份和年份似乎排序正确,但我在按类型排序时遇到了问题。在 compareTo 中按字符串排序的正确方法是什么?

目前的结果:

  • 论文 2012 12
  • 论文 2011 9
  • 在线 2013 4
  • 在线 2013 1
  • 在线 2009 年 11
  • 在线 2008 4

这是我的方法(我为它有点古怪而道歉,我一直在尝试不同的方法来按类型排序并且正在试验):

@Override
public int compareTo(Article rs) {

    Integer x = 0;


        Integer year1 = 0;
        Integer year2 = 0;
        Integer month1 = 99999;
        Integer month2 = 99999;
        Integer type1 = 99999;
        Integer type2 = 99999;

        if(rs.year != null && year != null) {

            if(!rs.year.equals(""))
                year1 = Integer.parseInt(rs.year);
            if(!year.equals(""))
                year2 = Integer.parseInt(year);
        }

        if(rs.month != null && month != null) {
            if(!rs.month.equals(""))
                month1 = Integer.parseInt(rs.month);
            if(!month.equals(""))
                month2 = Integer.parseInt(month);
        }

        if(rs.type == null)
            type1 = 99999;
        else
            type1 = 0;

        if(type == null)
            type2 = 99999;
        else
            type2 = 0;

        x = type2.compareTo(type1);
        if(x != 0) {
            return x;
        }

        x = year1.compareTo(year2);
        if(x != 0) {
            return x;
        }

        x = month1.compareTo(month2);
        if(x != 0) {
            return x;
        }


    return x;
}

最佳答案

我会重构(例如丢弃)您的完整代码并使用 CompareToBuilder 替换它.

这将创建以下代码:

enum ArticleType {
    ONLINE, PAPER
}

class Article implements Comparable<Article> {

    int year;
    int month;
    ArticleType type;

    Article(int year, int month, ArticleType type) {
        this.year = year;
        this.type = type;
        this.month = month;
    }

    @Override
    public int compareTo(Article o) {
        return new CompareToBuilder()
                .append(this.type, o.type)
                .append(this.year, o.year)
                .append(this.month, o.month)
                .toComparison();

    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("month", month)
                .add("year", year)
                .add("type", type)
                .toString();
    }
}

@Test
public void testSortArticles() throws Exception {
    List<Article> articleList = new ArrayList<>();
    articleList.add(new Article(2012, 1, ArticleType.ONLINE));
    articleList.add(new Article(2011, 1, ArticleType.ONLINE));
    articleList.add(new Article(2011, 6, ArticleType.ONLINE));
    articleList.add(new Article(2010, 1, ArticleType.ONLINE));
    articleList.add(new Article(2010, 1, ArticleType.PAPER));
    articleList.add(new Article(2010, 2, ArticleType.PAPER));
    articleList.add(new Article(2010, 3, ArticleType.PAPER));
    articleList.add(new Article(2012, 1, ArticleType.PAPER));
    articleList.add(new Article(2012, 9, ArticleType.PAPER));

    Collections.sort(articleList);

    System.out.println(articleList);
}

打印这将导致以下结果:

[Article{month=1, year=2010, type=ONLINE}, Article{month=1, year=2010, type=PAPER},
 Article{month=2, year=2010, type=PAPER}, Article{month=3, year=2010, type=PAPER},
 Article{month=1, year=2011, type=ONLINE}, Article{month=6, year=2011, type=ONLINE},
 Article{month=1, year=2012, type=ONLINE}, Article{month=1, year=2012, type=PAPER},
 Article{month=9, year=2012, type=PAPER}]

Will 提供了一个排序良好的列表。离线/在线也使用枚举 (ArticleType) 进行排序。在我看来,这看起来比您当前的实现要好一些。

关于java - 使用 compareTo 对对象进行排序(多种类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17536030/

相关文章:

java - 使用 ChangeListener 触发 Java Swing 中的更改?

java - mybatis IllegalArgumentException : Mapped Statements collection does not contain value

java - JTextArea JLabel 比较两个文本。文件逐行

java - 使用 sort 方法对人员数组进行排序

java - 编译程序后如何从源文件夹中获取 jar?

java - 扩展配置器中的 Spring Boot @Autowired null

java - 列计数与第 1 行的值计数不匹配 - Java SQL

java - 按字母顺序插入链表

java - 对对象数组进行排序时出错

java - compareTo 与 heapSort 的泛型