String Comporator to sort collection efficiently

Below is small code to implement String sorting using collection.sort(collectionName,comparatorForsoring)
It does not return any thing, it works on the collection object which is passed as argument.

public class StringPropertyComparator implements Comparator{

private String property;
private boolean sortAsec;

public StringPropertyComparator(String property,boolean sort)
{
this.property=property;
this.sortAsec=sort;
}

public int compare(Object o1, Object o2) {
Object src = PropertyResolver.getValue(property, o1);
Object compareTo = PropertyResolver.getValue(property, o2);
String src1=src==null ? “”:src.toString();
String compareTo1=compareTo==null ? “”:compareTo.toString();
return sortAsec ? src1.compareToIgnoreCase(compareTo1):compareTo1.compareToIgnoreCase(src1);
}
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s