i have a list of sub family names which i am sorting using list.sort()
The subfamily names are like
familyname - 1 - parts familyname - 2 - parts1 familyname - 3 - parts2 familyname - 11 - parts2
So when i sort, the result is like this
familyname - 1 - parts familyname - 11 - parts2 familyname - 2 - parts1 familyname - 3 - parts2
We have familyname - 11 - parts2 coming after familyname - 1 - parts which is understandable as its sorting as a string.
Is there workaround over this? One way i am thinking is to split the string, store the number into a map with number and the family name. But this approach would fail, when i have another like familyname2 - 1 - parts
Any thoughts?
prady,
I think what you need is a wrapper class around these strings that implements the Comparable interface. Then you can implement the compareTo method to return exactly what you need. You'll need to load your list with this wrapper class objects and then invoke sort.
public Integer compareTo(Object compareToPartObj) {
PartsString compareToPart = (PartsString)compareToPartObj;
// The return value of 0 indicates that both elements are equal.
Integer returnValue = 0; // equal
if (your comparison here) {
// Set return value to a positive value.
returnValue = 1; // this greater than other
} else if (some other comparison) {
// Set return value to a negative value.
returnValue = -1; //other > this
}
return returnValue;
}
Best,
Ram