我有一个车辆清单。我想根据品牌对这些车辆进行分类。顺序在另一个数组中定义。
此代码与两个品牌“Honda”和“Kia”的数组很好地分类。这与本田在起亚之上的排序在其他之上。是否有可能使其对大小会发生变化的数组通用。如果阵列是“雪佛兰”、“道奇”、“福特”怎么办?
谢谢
//This sorts Honda first, Kia second, and others after.
final String[] makes = new String[]{"Honda","Kia"};
Collections.sort(vehicles, new Comparator<Vehicle>() {
    @Override
    public int compare(Vehicle o1, Vehicle o2) {
        String makeObj1 = o1.getModel().getMakeName().toLowerCase();
        String makeObj2 = o2.getModel().getMakeName().toLowerCase();
        //honda first
        if (makeObj1.equals(makes[0].toLowerCase())) {
            if (makeObj2.equals(makes[0].toLowerCase())) {
                return 0;//honda = honda
            }
            if (makeObj2.equals(makes[1].toLowerCase())) {
                return -1;//honda > kia
            } else {
                return -1;//honda > others
            }
        }
        //kia first
        if (makeObj1.equals(makes[1].toLowerCase())) {
            if (makeObj2.equals(makes[0].toLowerCase())) {
                return 1;//kia < honda
            }
            if (makeObj2.equals(makes[1].toLowerCase())) {
                return 0;//kia = kia
            } else {
                return -1;//kia > others
            }
        }
        //honda second
        if (makeObj2.equals(makes[0].toLowerCase())) {
            if (makeObj1.equals(makes[1].toLowerCase())) {
                return 1;//kia < honda
            } else {
                return 1;//other < honda
            }
        }
        //kia second
        if (makeObj2.equals(makes[1].toLowerCase())) {
            return 1;//others < kia
        }
        return 0;//all cases should been covered
    }
});