Define a class to hold your item in. Seems like you want it to be a String.
For that class, you need to define the Comparable interface and put the logic to compare in its abstract method.
int compareTo(T o)
For example:
class MyString extends String
{
@Override
int compareTo(Object obj)
{
// put your logic in here.
// Return -1 if this is "less than" obj.
// Return 0 if this is equal to obj
// Return 1 if this is "greater than" obj.
// Test length first
if (length() < obj.length())
return -1;
if (length() > obj.length())
return 1;
// Lengths are the same, use the alphabetical compare defined by String already
return super.compareTo(obj);
}
}
Disclaimer, I didn't actually test this code, but it should be close to what you want.