[Solved] sort int string java [closed]


The reasons for this question getting downvoted are:

  1. There’s no effort shown. Tell us what you’ve tried and what you know.
  2. We love code. Please show us your code. Even the non-working one.
  3. 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 than 5051?

That said, here’s your answer:

  1. Read the cars into some sort of structure. A Car class would be perfect (could have its own compareTo() method if this kind of sort is the only useful one). Or just use a String[] for each car, therefore ending up with an array of cars (String[][]).
  2. 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]