1

这是我的代码:

import java.util.List;

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.bc.filter.SearchRequestService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.issue.search.SearchRequest;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.web.bean.PagerFilter;


class IssueTest{

    String  id;
    String type;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

    public IssueTest(String id,String type)
    {
        this.id = id;
        this.type = type;

    }


}


public class test {

    /**
     * @param args
     */

     static List<IssueTest> issues; 


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        test t = new test();


    }

    public test ()
    {

        IssueTest i = new IssueTest("B-1", "bug");
        issues.add(i);
        IssueTest j = new IssueTest("S-1", "story");
        issues.add(j);
        IssueTest k = new IssueTest("T-1", "task");
        issues.add(k);
        IssueTest l = new IssueTest("T-2", "task");
        issues.add(l);
        IssueTest m = new IssueTest("E-1", "epic");
        issues.add(m);
        IssueTest n = new IssueTest("E-2", "epic");
        issues.add(n);
        IssueTest o = new IssueTest("T-3", "task");
        issues.add(o);
        IssueTest p = new IssueTest("S-2", "story");
        issues.add(p);
        IssueTest q = new IssueTest("B-2", "bug");
        issues.add(q);
        IssueTest r = new IssueTest("B-3", "bug");
        issues.add(r);

    }


}

我想根据“类型”对这个列表进行排序。我想要所有对象的顺序 -任务然后错误然后故事然后史诗

4

4 回答 4

3

您将需要编写一个自定义Comparator来实现该排序。不幸的是,如果没有第三方库,这将有点复杂……尤其是如果您不在 Java 7 上。

Comparator<IssueTest> comparator = new Comparator<IssueTest>() {
  private int getStringPosition(String str) {
    if(str.equals("task")) {
      return 0;
    } else if (str.equals("bug")) {
      return 1;
    } else if (str.equals("story")) {
      return 2;
    } else if (str.equals("epic")) {
      return 3;
    } else {
      throw new AssertionError();
    }
  }
  public int compare(IssueTest a, IssueTest b) {
    return getStringPosition(a.getType()) - getStringPosition(b.getType());
  }
};
Collections.sort(list, comparator);

但是,如果您使用枚举而不是字符串类型,这会容易得多。

如果您可以使用第三方库,那么使用 Guava 可能会更简单一些:

Ordering<String> typeOrdering = Ordering.explicit("task", "bug", "story", "epic");
Ordering<IssueTest> issueTestOrdering =
 typeOrdering.onResultOf(new Function<IssueTest, String>() {
   public String apply(IssueTest it) {return it.getType(); }
 });
Collections.sort(list, issueTestOrdering);

(披露:我为 Guava 做出了贡献。)

于 2012-08-01T12:41:58.167 回答
2

例如,您可以定义一个比较器方法并使用Collections.sort(lst)

于 2012-08-01T12:41:13.830 回答
2

如果这是您想要对对象列表进行排序的唯一IssueTest方法,那么正确的方法是实现Comparable.

http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html上有很好的解释

如果您想在不同的地方按不同的键进行排序,请不要制作IssueTestimplement Comparable,而是制作一组实现 implement 的类Comparator<IssueTest>

于 2012-08-01T12:41:56.273 回答
1
public class IssueComparator implements Comparator<IssueTest>{

    @Override
    public int compare(IssueTest issue1, IssueTest issue2) {

        String type1 = issue1.getType();
        String type2 = issue2.getType();

        if (type1.length() > type2.length() ){
            return +1;
        }else if (type1.length() < type2.length() ){
            return -1;
        }else{
            return 0;
        }
    }

显然长度不是一个合适的使用标准,以上是为了说明如何Comparator完成一个实现。

于 2012-08-01T12:44:04.383 回答