【问题标题】:Sorting an ArrayList using content in the ArrayList in Java在 Java 中使用 ArrayList 中的内容对 ArrayList 进行排序
【发布时间】:2016-02-10 23:16:18
【问题描述】:

我有一个 ArrayList "R",它是 WordRanking 类的对象集合。 WordRanking 类中有 2 个变量。 1.int频率 2.串词

我正在尝试使用频率对 ArrayList R 进行排序

【问题讨论】:

标签: java collections


【解决方案1】:

最好的方法是创建一个自定义比较器来定义 WordRanking 对象之间的关系,然后使用 Collections.sort(list, comparator); 调用。

它应该看起来像这样

public class SomeComarator implements Comparator<WordRanking > {
      @Override
      public int compare(ActiveAlarm x, ActiveAlarm y) {
        // return 1 if x>y , -1 if y>x , and 0 if equals
      }
    }

SomeComarator wordComperator = new SomeComarator ();
Collections.sort(R, wordComperator );`

附带说明 - 请不要使用像“R”这样的变量名。
而且,你知道,谷歌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2012-09-10
    • 2019-10-05
    相关资源
    最近更新 更多