java-使用可比较或比较器接口使用字符串s1的顺序对字符串s2进行排序

我有两个字符串s1和s2,我想根据s1中字母的出现顺序对s2进行排序,如果s2中还剩下其他字母,则按字母顺序对它们进行排序.

假设我有以下内容;

字符串s1 =“ war”;

字符串s2 =“作为一名程序员真是太棒了”;

输出:waaarrrIbeeeeggimmmnoopsst.

我已经写了一个代码来做到这一点,尽管我想知道是否有可能使用比较器/可比较接口来解决它.

下面列出的是我的代码段.

public class Sort {

    private static String a = "war";
    private static String b = "Its awesome being a programmer";
    static List<Character> list = new ArrayList<>();

    static public void main(String[] args) {
        Character s;
        Character x;

        System.out.println("String to be sorted: '" + b + "'");
        System.out.println("Key for sort: '" + a + "'");

        /* 
         * put all the string in a list 
         */
        for (int i = 0; i < b.length(); i++) {
            s = b.charAt(i);
            if (s != ' ') {
                list.add(s);
            }
        }
        /* 
         * compare individual chac in key with individaul char in string to sort 
         */
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < a.length(); j++) {
            x = a.charAt(j);
            for (int k = 0; k < b.length(); k++) {
                s = b.charAt(k);
                if (x == s) {
                    sb.append(s);
                    list.remove(x);
                }
            }
        }

        /* 
         * check if list is empty if not, sort and append the rest to the stringbuilder 
         */
        if (!list.isEmpty()) {
            Collections.sort(list);
            for (char c : list) {
                sb.append(c);
            }
        }
        System.out.println("Sorted version of string: '" + sb.toString() + "'");
    }
}

解决方法:

private static String a = "war";
private static String b = "Its awesome being a programmer".replace(" ","");
private static String answer = "waaarrrIbeeeeggimmmnoopsst";

public static void main(String[] args) {
    List<String> characters = new ArrayList<String>(b.length());
    for (int i=0;i<b.length();i++){
        characters.add(String.valueOf(b.charAt(i)));
    }
    Collections.sort(characters,new CompareIt(a));
    String sortedString = listToString(characters);
    System.out.println(sortedString);
    System.out.println(answer);
    System.out.println(answer.equals(sortedString));
}
private static String listToString(List<String> listOfStrings){
    StringBuilder builder = new StringBuilder();
    for (String str : listOfStrings){
        builder.append(str);
    }
    return builder.toString();
}
private static class CompareIt implements Comparator<String>{

    private final String source;

    public CompareIt(String source) {
        super();
        this.source = source;
    }

    public int compare(String o1, String o2) {
        int i1 = source.indexOf(o1);
        int i2 = source.indexOf(o2);
        if (i1==-1 && i2!=-1){
            return 1;
        } else if (i1!=-1 && i2==-1){
            return -1;
        } else if (i1!=-1 && i2!=-1){
            return i1 > i2 ? 1:-1;
        } else {
            return o1.compareTo(o2);
        }
    }

}

这似乎有效.
编辑:要包含结果与预期的问题答案匹配的sysout.
EDIT2:我有最终索引比较的错字? 1:0代替1:-1.

上一篇:c – 使用std :: set find和struct data


下一篇:java-使用Collections.sort()对ArrayList进行复杂排序?