这是我的代码:
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);
}
}
我想根据“类型”对此列表进行排序。我想要所有物品 按顺序 - 任务然后错误然后故事然后史诗
答案 0 :(得分: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捐款。)
答案 1 :(得分:2)
您可以定义比较器方法并使用Collections.sort(lst)
,例如。
答案 2 :(得分:2)
如果这是 only 方式,您希望对IssueTest
个对象的列表进行排序,那么正确的方法是实现Comparable
。
http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
对此进行了详细解释如果您希望按不同位置的不同键进行排序,请不要使IssueTest
实现Comparable
,而是创建一组实现Comparator<IssueTest>
的类。
答案 3 :(得分: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
的实现。