In this case, the desired order is the natural (alphabetical) order, so you can use the Arrays.sort(Object\[\])
method. If the desired order does not match the natural order, you can implement a Comparator
and use the Arrays.sort(String\[\], Comparator<String>)
method.
An example for the Comparator
implementation (just to show how it could work):
public class SemesterComparator implements Comparator<String> {
private static final List<String> SEASONS = Arrays.asList("Spring", "Summer", "Fall", "Winter");
@Override
public int compare(String one, String two) {
String[] partsOne = one.split(" ");
String[] partsTwo = two.split(" ");
if (partsOne.length != 2 || partsTwo.length != 2) {
throw new IllegalArgumentException();
}
if (!SEASONS.contains(partsOne[0]) || !SEASONS.contains(partsTwo[0])) {
throw new IllegalArgumentException();
}
// compare years
int comparison = partsOne[1].compareTo(partsTwo[1]);
if (comparison == 0) {
// if years are equal: compare season
comparison = SEASONS.indexOf(partsOne[0]).compareTo(SEASONS.indexOf(partsTwo[0]));
}
return comparison;
}
}
But as already mentioned in the comments, it would be better to store the values in a format or class that is better sortable.