Sorting sights – by distance, or alphabetically

All sights in Dublin Buzz can be sorted by the user, in one of three ways – alphabetically, by distance, or by “popularity”. (I define popularity somewhat subjectively!). In any case, it requires a little Java trick combined with a well-known design pattern to make this work. Here is the code for sorting by distance, for example:

     * User clicked distance button
     * Sorts by distance, nearest to furthest. 
     * @param v
	public void ShowDistance (View v)
    	viewable = getDisplaySights(app.sights);
        displaySights (new Comparator<Sight>(){
            public int compare(Sight p1, Sight p2) {
                if (p1.getLatestDistance() > p2.getLatestDistance())
                	return 1;
                if (p1.getLatestDistance() < p2.getLatestDistance())
                	return -1;
                return 0;		                

This code makes use of anonymous classes to pass a new Comparator object (which has no name, hence is anonymous) to the displaySights() method. displaySights() uses this comparator to do the sorting of the list of sights. In this example, the comparator sorts sights by comparing their distances to the user. The getLatestDistance() method retrieves the last known distance (in meters) for the sight to the user. This is updated by a different part of the code as GPS coordinates change. All comparators work by returning 1 when the first argument is greater than the second argument, -1 when it is smaller, and 0 when they are equal. The sort algorithm borrows this comparator to apply sorting logic. This is an example of the Strategy design pattern at work. In the Strategy pattern, the official definiton (by Gang of Four) is “Define a family of algorithms, encapsulate each one, and make them interchangeable. Lets the algorithm vary independently from clients that use it.” Here, the algorithm that varies is the algorithm for comparing two sights. The client in this case is my code for sorting them (displaySights()) and the user of the algorithms is the Collections.sort() method.

    public void displaySights (Comparator<Sight> compare)
        // The order this list by ordering preferences, passed via the comparator, if specified
        if (compare != null)
        	Collections.sort(viewable, compare);

You simply pass in a different comparator if you want to sort on some other criteria. In fact, the criteria to sort on can be based on any custom ordering you can think of. It is an incredibly powerful and flexible way to change (dynamically) the sorting behaviour you want.

Here’s the code for sorting alphabetically. Notice how it is very similar to the method for sorting by distance, but it just forces the use of a string comparison on the name.

    public void ShowAToZ (View v)
    	viewable = getDisplaySights(app.sights);
        displaySights(new Comparator<Sight>(){
            public int compare(Sight p1, Sight p2) {
                return (p1.getName().compareTo(p2.getName()));
This entry was posted in Android, Android development, Mobile Apps. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s