Listing 23.3 Error in Java 5


#1

What’s the go with listing 23.3?
According to listing 23.3 we have to make an Instance of Comparator
However according to Java in a nutshell 5th edition
Comparator is defined as an INTERFACE (as of Java 5)

Here is the full definition for Comparator

public interface Comparator<T>
{
//Public instance methods
int compare(To1,To2);
boolean equals(Object obj);
}

I think that I will solve this problem by creating the comparator in an inner class or maybe
ill create a separate comparator class

I decided to make a separate comparator class and I have posted it here for any interested person

/**
 * This is a comparator which compares packages by 
 * their package name
 * 
 * Input1: Activity (Used by package manager to find labels)
 * Outputs: String (sorted list)
 */

public class LaunchActivityComparator implements Comparator<ResolveInfo>
{
//The package manager needs this activity to find the package labels 
private Activity activity;
	
	public LaunchActivityComparator(Activity a)
	{activity=a;}
	
	//The package manager uses 1)the Activity to find 2) the labels of 2 different packages
	public int compare(ResolveInfo a, ResolveInfo b)
	{
	PackageManager pm=getActivity().getPackageManager();
	//This is where we find the order
	return String.CASE_INSENSITIVE_ORDER.compare(a.loadLabel(pm).toString(), b.loadLabel(pm).toString());
	}

	//This overrides the usual getActivity() method
	private Activity getActivity()
	{return activity;}
}