1

Here is my code :

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);

    }


}

I want to sort this list according to the "type" . I want all objects in the order of - tasks then bugs then story then epic

1
  • and the results you have gotten so far are what? Further what have you tried to validate that your implementation is correct / incorrect? Commented Aug 1, 2012 at 12:40

4 Answers 4

3

You're going to need to write a custom Comparator to implement that ordering. This is going to be a bit complicated, unfortunately, without third-party libraries...especially if you're not on 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);

If you used an enum instead of string types, this would be much easier, though.

If you can use third-party libraries, this might be a bit simpler with 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);

(Disclosure: I contribute to Guava.)

Sign up to request clarification or add additional context in comments.

1 Comment

I cannot use external libraries . Please can you show without this
2

You can define a comparator method and use Collections.sort(lst), for example.

Comments

2

If that's the only way you want to ever sort a list of IssueTest objects, then the right way would be to implement Comparable.

It is well explained on http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

If you'd want to sort by different keys in different places, don't make IssueTest implement Comparable, but make a set of classes that implement Comparator<IssueTest>.

Comments

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;
        }
    }

Obviously length is not an appropriate criterion to use, The above is to illustrate how an implementation of Comparator can be completed.

2 Comments

It makes a lot more sense to use the compareTo method already present on the String class; type1.compareTo(type2). Of course, you may want to check for nulls and you may want to store it so you can check the other fields if the result is null.
@Thor84no sure equally valid point, I am assuming that String is the correct object type in his domain object. I did however want to give OP an explanation as to how Comparator can be implemented as it is important to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.