The reasons for this question getting downvoted are:
- There’s no effort shown. Tell us what you’ve tried and what you know.
- We love code. Please show us your code. Even the non-working one.
- If one thinks about the problem for a while, unanswered questions begin to pop up. For example:
- Do all the numbers have 6 digits?
- If not, is
05050
less or more than5051
?
That said, here’s your answer:
- Read the cars into some sort of structure. A
Car
class would be perfect (could have its owncompareTo()
method if this kind of sort is the only useful one). Or just use aString[]
for each car, therefore ending up with an array of cars (String[][]
). - Sort them like this if they’re Comparable, or like this using a custom Comparator.
An example (use a SkodaCar
class instead of that String[]
thing if you can):
// every car is a String[3]
String[][] cars = loadCars();
Comparator<String[]> skodaCarComparator = new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
// compares the numbers as Strings using String's compareTo()
return o1[2].compareTo(o2[2]);
}
};
Arrays.sort(cars, skodaCarComparator);
solved sort int string java [closed]