Home > Back-end >  How would I sort an arraylist of strings that's formatted as a height in java?
How would I sort an arraylist of strings that's formatted as a height in java?

Time:11-12

    ArrayList<String> heights = new ArrayList<String>();

    Comparator<String> setTableNumber = new Comparator<String>(){
          @Override
          public int compare(String o1, String o2){{       
                 return (o1).compareTo(o2);}}};

    Collections.sort(players, setTableNumber);

Suppose heights consists Strings that are like this ["5'11"", "6'11"","4'2""]

And I want it to sort from highest to lowest like ["6'11"","5'11"","4'2""]

I'm thinking about using .split('), converting the strings to integers and then multiplying the feet by 12 and then adding that to the second string (after I trim off the ") and comparing two values as inches. But is there a better way to do it?

Thanks in advance.

CodePudding user response:

Personally, I'd convert the String values to int (inches) up-front, this gives you the opportunity to deal with possible errors during conversation.

ArrayList<String> heights = new ArrayList<String>();
heights.add("5'11");
heights.add("6'11");
heights.add("5'2");
heights.add("4'2");

Comparator<String> setTableNumber = new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        Integer lhs = convertToInches(o1);
        Integer rhs = convertToInches(o2);
        if (lhs == null) {
            return 1;
        } else if (rhs == null) {
            return -1;
        }
        return lhs.compareTo(rhs);
    }

    protected Integer convertToInches(String value) {
        String parts[] = value.split("'");
        if (parts.length != 2) {
            return null;
        }
        return (Integer.parseInt(parts[0]) * 12)   Integer.parseInt(parts[1]);
    }
};

Collections.sort(heights, setTableNumber);
System.out.println(heights);

which prints

[4'2, 5'2, 5'11, 6'11]

This workflow will place "bad" values to the end of the list, for example...

ArrayList<String> heights = new ArrayList<String>();
heights.add("Worse");
heights.add("5'11");
heights.add("6'11");
heights.add("5'2");
heights.add("Bad");
heights.add("4'2");

will generate

[4'2, 5'2, 5'11, 6'11, Worse, Bad]

But personally, I'd deal with this before trying to sort the list (by converting the values to int). You can then do some additional formatting to the int values when you want to print it

CodePudding user response:

Try this.

public static void main(String[] args) {
    List<String> heights = List.of("5'11\"", "6'11\"","4'2\"");

    List<String> sorted = heights.stream()
        .map(s -> new Object() {
            String feetInch = s;
            int inch;
            {
                String[] f = feetInch.replaceFirst("\"", "").split("'");
                inch = Integer.parseInt(f[0]) * 12   Integer.parseInt(f[1]);
            }
        })
        .sorted(Collections.reverseOrder(Comparator.comparing(obj -> obj.inch)))
        .map(obj -> obj.feetInch)
        .toList();

    System.out.println(sorted);
}

output:

[6'11", 5'11", 4'2"]
  • Related