首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多字段集合排序

多字段集合排序
EN

Stack Overflow用户
提问于 2013-07-11 12:07:17
回答 3查看 118关注 0票数 0

我有一个类Arraylist包含值字符串字,字符串扩展字,双置信度,双支持

我想根据置信度对数组列表进行排序,然后根据支持度对其进行排序。

我已经成功地根据置信度对arraylist进行了排序,但我未能提出一种新的方法来根据支持度对arraylist进行排序

这是我根据置信度对其进行排序的代码

代码语言:javascript
复制
public class ExpandedTerm implements Comparable<ExpandedTerm> {
String word;
String expandedWord;
double support;
double confidence;

public ExpandedTerm (String word,String expandedWord, double confidence,double support){
    this.word = word;
    this.expandedWord = expandedWord;
    this.support = support;
    this.confidence = confidence;
}

public String getWord(){
    return word;
}

public String expandedWord(){
    return expandedWord;
}

public Double getSupport(){
    return support;
}

public Double getConfidence(){
    return confidence;
}

@Override
public int compareTo(ExpandedTerm conf) {
    return new Double(this.confidence).compareTo(new Double(conf.confidence));
}

我未能创建另一个方法,如compareTo,根据支持值对其进行排序。如何首先按置信度对其进行排序,然后再创建另一种方法来按支持度对其进行排序?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-11 12:09:58

这个的用户比较器。As compaarble提供了基于单一类型排序的功能。这里有一个链接,您可以在这里找到何时使用比较和比较程序

http://iandjava.blogspot.in/2012/10/comparable-and-comparator.html

使用多个比较函数

用于信心的

  • one

代码语言:javascript
复制
    public class ConfidanceComparator implements Comparator<ExpandedTerm> {
        @Override
        public int compare(final ExpandedTerm  o1, final ExpandedTerm  o2) {
            return new Double(o1.confidence).compareTo(new Double(o2.confidence));
        }
    }

用于支持的

  • one

代码语言:javascript
复制
    public class SupportComparator implements Comparator<ExpandedTerm> {
        @Override
        public int compare(final ExpandedTerm  o1, final ExpandedTerm  o2) {
            return new Double(o1.support).compareTo(new Double(o2.support));
        }
    }

并使用Collections.sort(<List>, <comparator>),您将获得所需的列表。

仅当您想要基于信任或支持进行排序时,才需要THis。

但是如果你需要的话,那么首先在信任的基础上排序,如果信任是相等的,那么检查支持基础。那么可比就足够了,并且是

代码语言:javascript
复制
public int compareTo(ExpandedTerm conf) {
    int compare = new Double(this.confidence).compareTo(new Double(conf.confidence));

    if (compare == 0) {
        compare = new Double(this.support).compareTo(new Double(conf.support));
    }
    return compare;
}
票数 3
EN

Stack Overflow用户

发布于 2013-07-11 12:15:14

在您的compareTo方法中尝试以下代码:

代码语言:javascript
复制
@Override
public int compareTo(ExpandedTerm other) {
    Double thisConfidence = new Double(getConfidence());
    Double otherConfidence = new Double(other.getConfidence());
    int compare = thisConfidence.compareTo(otherConfidence);

    if (compare == 0) {
        Double thisSupport = new Double(getSupport());
        Double otherSupport = new Double(other.getSupport());
        compare = thisSupport.compareTo(otherSupport);
    }
    return compare;
}

基本上,只有在“置信度”相等的情况下才比较“支持”。

票数 1
EN

Stack Overflow用户

发布于 2013-07-11 13:13:00

我看到您的回复,您希望排序一次,然后再排序,除非不同,所以我假设您希望在排序时添加一个自定义比较器。这就是你要找的吗?

代码语言:javascript
复制
public static void main(String[] args) {
    ExpandedTerm term1 = new ExpandedTerm("a", "b", 1, 4);
    ExpandedTerm term2 = new ExpandedTerm("c", "d", 3, 2);

    List<ExpandedTerm> list = new ArrayList();
    list.add(term1);
    list.add(term2);

    Collections.sort(list);
    System.out.println(list);

    Collections.sort(list, new Comparator<ExpandedTerm>() {
        @Override
        public int compare(ExpandedTerm o1, ExpandedTerm o2) {
            return new Double(o2.confidence).compareTo(new Double(o1.confidence));
        }
    });
    System.out.println(list);
}

下面是输出

代码语言:javascript
复制
[ExpandedTerm@2eeb3c84, ExpandedTerm@55d2162c]
[ExpandedTerm@55d2162c, ExpandedTerm@2eeb3c84]

其他一些技巧:确保为ExpandedTerm实现了toString()、hashCode()和equals()函数。这些对于调试以及在HashMap等其他集合中的使用都是必不可少的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17584878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档