1

I have two objects which have the following structure:

public class Event{
    private String name;
    private int id;
    private List<Tags> tags;
}

public class Tags{
    private String tagName;
    private int rank;
}

I have a list of Event objects

 List<Event> events = new ArrayList<Event>();

I want to sort the events based on the rank in the Tags object.

For Example,

 Tags tag1 = new Tags("web", 1);
 Tags tag2 = new Tags("mobile", 7);
 List<Tags> tags = new ArrayList<Tags>();
 tags.add(tag1);
 tags.add(tag2);
 Event event1 = new Event("test1",4242432, tags);

 Tags tag3 = new Tags("web", 2);
 Tags tag4 = new Tags("mobile", 8);
 List<Tags> tags1 = new ArrayList<Tags>();
 tags.add(tag3);
 tags.add(tag4);
 Event event2 = new Event("test2",546423, tags1);

So now I want to sort these two events based on the rank field within the Tags object. Since Tags is an ArrayList there can be multiple ranks, so I want to pass an argument, tagName, which will decide which ranks to compare within different Tags objects.

5
  • 7
    This is what the Comparator interface is for. Implement a Comparator<Event> that returns -1, 0 or 1 when comparing two Event objects. However, you have to decide what you mean by "based on the rank field within the Tags object". Each Event contains potentially more than one Tags object. Also, we're not going to write it for you. Post what you have written so far. Commented Feb 21, 2014 at 20:16
  • Implement a custom sort algorithm. Commented Feb 21, 2014 at 20:18
  • 5
    @nikpon That is a quite bad advice in Java. Simply implementing Comparator is much more standard, probably less bugs, less tests. Commented Feb 21, 2014 at 20:20
  • 1
    @nikpon ok, probably less performant too, but that also requires tests. So I would still not suggest using custom sorting algorithm. This is not one of use cases of the O(1) sort algorithms. Commented Feb 21, 2014 at 20:40
  • 2
    @nikpon Here the problem is "How to write some code to compare two Event objects based on some tagName"? and not "How to sort list of Event?...... To perform any kind of sort, you should first have a way to compare two similar objects. Which sorting algo to use is a next question. Commented Feb 21, 2014 at 20:48

1 Answer 1

3

Use java.lang.Comparator interface.

public class EventComparator implements Comparator<Event> {

    private String tagName;

    public EventComparator(String tagName) {
        this.tagName = tagName;
    }

    @Override
    public int compare(Event e1, Event e2) {
        Tag t1 = getTag(e1, tagName); // getTag(...) returns the e1's tag with name 'tagName'
        Tag t2 = getTag(e2, tagName);

        // Ignoring null check for brevity
        if (t1.getRank() > t2.getRank())
            return 1;
        else if (t1.getRank() < t2.getRank())
            return -1;
        else
            return 0;
    }
}

Usage:

Collections.sort(eventsList, new EventComparator("web"));
Collections.sort(eventsList, new EventComparator("mobile"));
Sign up to request clarification or add additional context in comments.

2 Comments

Probably it might make sense prioritizing based on multiple tags too. But that is easy to implement.
That's the kind of answers that is really nice to see. Short, simple and to the point.

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.