In the below code, I want to understand what does the Collections.sort function is doing. What does this means
Collections.sort(list, new Comparator() { a complete java function });
I understand that new Comparator() means a class object, but what is the purpose of the function block here, and what we call it in java.
Thanks.
/**
* Sorts/shuffles the given list according to the current sending queue
* mode. The list can contain either Message or Tuple<Message, Connection>
* objects. Other objects cause error.
* @param list The list to sort or shuffle
* @return The sorted/shuffled list
*/
@SuppressWarnings(value = "unchecked") /* ugly way to make this generic */
protected List sortByQueueMode(List list) {
switch (sendQueueMode) {
case Q_MODE_RANDOM:
Collections.shuffle(list, new Random(SimClock.getIntTime()));
break;
case Q_MODE_FIFO:
Collections.sort(list,
new Comparator() {
/** Compares two tuples by their messages' receiving time */
public int compare(Object o1, Object o2) {
double diff;
Message m1, m2;
if (o1 instanceof Tuple) {
m1 = ((Tuple<Message, Connection>)o1).getKey();
m2 = ((Tuple<Message, Connection>)o2).getKey();
}
else if (o1 instanceof Message) {
m1 = (Message)o1;
m2 = (Message)o2;
}
else {
throw new SimError("Invalid type of objects in " +
"the list");
}
diff = m1.getReceiveTime() - m2.getReceiveTime();
if (diff == 0) {
return 0;
}
return (diff < 0 ? -1 : 1);
}
});
break;
/* add more queue modes here */
default:
throw new SimError("Unknown queue mode " + sendQueueMode);
}
return list;
}
I finally got he understanding of comparator. This simple example would help:
Collections.sort(ls, new Comparator()
{
public int compare(Object o1, Object o2)
{
String sa = (String)o1;
String sb = (String)o2;
int v = sa.compareTo(sb);
return v;
// it can also return 0, and 1
}
}
);
The
Collections.sortmethod sorts the collection given as the parameter. By default the natural order of the collection is used. The natural order is defined by the objects in the collection.For example, when you have a collection of
Strings, the sort method sorts this collection in the alphabetical order of the Strings. How does thesortmethod knows that it has to sort alphabetically? It used the compareTo method in theStringclass.compareTocompares the current object (this) with another objects and decides which comes first between these two. UsingcompareToon all the items in the list,sortdetermines the correct order.So compareTo determines the natural order of an object.
What if you want some other ordering other than the natural order? Say you want to sort a list of Strings in reverse alphabetical order?
You can do that by another (overloaded) variant of the sort method. This variant accepts a second parameter, which is a instance of the Comparator interface.
So, you have to write your own class that implements Comparator, and so that has its own
compareTomethod. The sort method then uses thiscompareTorather than thecompareToin the object in the collection (String).So you say
and then
or
Collections.sort(myStringList, new MyStringComparator(););
See Object Ordering
If the
MyStringComparatoris going to be used only for this case, you would not want to create an separate class, give it a name, a file and so on. All you need is a something temporary that you can forget after the sort.So instead of writing all the code, you can say:
See, the second parameter is the class definition itself.
This is called an anonymous inner class. A kind of use and throw class, for such situations. You will see a lot of anonymous inner class in Swing event handlers.