javaroman-numerals

Sorting roman numeral in java


Input:

[IX, VIII]

Desired Output:

[VIII, IX]

The output of stage 0

[8, 9]

I create a function to return the decimal value of a roman numeral then I have another function for sorting the numbers according to the increasing order.

I am stuck on the point of how to return the sorted decimal vector to the roman number.

Here is a sample of my code.

static int romanToDecimal(String romanNumber){...
}

static List<String> sortedList(List<String> number) {
    Vector<Integer> v = new Vector<Integer>();
    int size = number.size();
    for (int i = 0; i < size; i++) 
        v.add(romanToDecimal(number.get(i)));
    Collections.sort(v);  //stage 0
}

Solution

  • You can use the Comparator util class to compare by the actual numeral value:

    static void sortRomanNumerals(List<String> romanNumerals){
        romanNumerals.sort(Comparator.comparing(Main::romanToDecimal));
    }