Untitled
unknown
plain_text
4 years ago
2.0 kB
5
Indexable
private static int getNumeric(String roman) {
Map<Character,Integer> romanList = new HashMap<>();
romanList.put('I', 1);
romanList.put('V', 5);
romanList.put('X', 10);
romanList.put('L', 50);
romanList.put('C', 100);
romanList.put('D', 500);
romanList.put('M', 1000);
int total = 0;
int temp = 0;
char[] chars = roman.toCharArray();
for(int i = (chars.length-1); i >= 0 ;i--){
if(temp <= romanList.get(chars[i])){
total += romanList.get(chars[i]);
} else {
total -= romanList.get(chars[i]);
}
temp = romanList.get(chars[i]);
}
return total;
}
private static int getOrder(String obj){
return getNumeric(obj.split(" ")[1]);
}
private static String getName(String obj){
return obj.split(" ")[0];
}
public static List<String> sortRoman(List<String> names) {
for(int i=0;i<names.size();i++){
for (int j=i+1;j<names.size();j++){
if(i!=j){
String temp="";
String name1 = getName(names.get(i));
int order1 = getOrder(names.get(i));
String name2 = getName(names.get(j));
int order2 = getOrder(names.get(j));
if(name1.compareTo(name2) > 0){
temp = names.get(i);
names.set(i, names.get(j));
names.set(j, temp);
}
}
else if(name1.compareTo(name2) == 0){
if(order1>order2){
temp=names.get(i);
names.set(i, names.get(j));
names.set(j, temp);
}
}
}
}
}
return names;
Editor is loading...