I have a list of items consists of digits and alphabets like below:
Original List:
Apple 1
Apple 1
Apple 4
Apple 1A
Apple 1B
Apple 1D
Apple 2A
Apple 2A
Apple 1C
Apple 1B
Apple 2C
Apple 10
Apple 11
Apple 5
Apple 11
Apple 8D
Banana 1
Banana 4
Banana 9D
Banana 9E
Banana 9C
Banana 13
Banana 16
It's a search result from a API but only sorted by alphabetical order of Apples and Bananas. Now I'd like to sort it by the digits (digits randomly come with letters A, B, C, D, E) like below:
The digits with A, B, C, D, E letters should be sorted by both digit and alphabetical order.
Expected list:
Apple 1
Apple 1
Apple 1A
Apple 1B
Apple 1B
Apple 1C
Apple 1D
Apple 2
Apple 2A
Apple 2A
Apple 2C
Apple 4
Apple 5
Apple 8D
Apple 10
Apple 11
Banana 1
Banana 4
Banana 9C
Banana 9D
Banana 9E
Banana 13
Banana 16
I have tried this solution, but it orders all items by the item begins with 0-9 digit like below:
private List<Location> sortLocationList(List<Location> locationArrayList){
Collections.sort(locationArrayList, new Comparator<Location>(){
@Override
public int compare(Location o1, Location o2){
return o1.getValue().compareToIgnoreCase(o2.getValue());
}
});
return locationArrayList;
}
public class Location{
public enum LocationType{ADDRESS, STREET, CITY}
private JavascriptObject object;
private LocationType locationType;
private String value;
public Location(LocationType locationType, String value, JavascriptObject object){
this.locationType = locationType;
this.value = value;
this.object = object;
}
}
My solution is returning this:
Apple 1
Apple 1
Apple 10
Apple 11
Apple 1A
Apple 1B
Apple 1B
Apple 1C
Apple 1D
Apple 2
Apple 2A
Apple 2A
Apple 2C
Apple 4
Apple 5
Apple 8D
Banana 1
Banana 13
Banana 16
Banana 4
Banana 9C
Banana 9D
Banana 9E
Any better solution to sort this list?
Thanks in advance.