2013-03-25 39 views
3

對字符串s2進行排序我有兩個字符串s1和s2,並且我想根據s1中字母的出現順序對s2進行排序,並且如果其他字母留在s2按字母順序排序。使用字符串s1的順序使用可比較的或比較器接口

假設我有以下;

String s1 =「war」;

String 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() + "'"); 
    } 
} 

回答

2
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。

+0

它似乎沒有給出所需的輸出 – Ayodeji 2013-03-25 15:45:05

+0

抱歉有什麼問題,當我運行它時,我得到[w,a,a,a,r,r,r,i,b,e,e,e ,e,g,g,i,m,m,m,n,o,o,p,s,s,t],這與你的上述結果相符 – BenG 2013-03-25 15:54:14

+0

這是我得到的結果[a,w, a,r,r,a,r,I,b,e,e,e,e,g,g,i,m,m,m,n,o,o,p,s,s,t] – Ayodeji 2013-03-25 16:12:55

1
public static void main(String[] args) { 
    String s1 = "war"; 
    String s2 = "Its awesome being a programmer"; 
    String result = ""; 
    for (int i = 0; i < s1.length(); i++) { 
     int len = s2.length() 
       - s2.replace(String.valueOf(s1.charAt(i)), "").length(); 
     s2 = s2.replace(String.valueOf(s1.charAt(i)), "").replace(" ", ""); 
     for (int j = 0; j < len; j++) 
      result = result + String.valueOf(s1.charAt(i)); 
    } 
    char[] remaining = s2.toCharArray(); 
    Arrays.sort(remaining); 
    for (Character c : remaining) 
     result = result + String.valueOf(c); 

    System.out.println(result); 
} 

試試這個:我試着不使用任何接口。

輸出:

waaarrrIbeeeeggimmmnoopsst 
+0

很酷,但不會太過頭了,因爲字符串應該是不可變的。另外,運行時間仍然是O(n^2)。我雖然可能使用任何這些接口,但我可以做得更好。 – Ayodeji 2013-03-25 15:18:16

+0

@Ayodeji我沒有完全讀你的問題,並開始編碼。 :( – 2013-03-25 15:21:14

1
public static Comparator<Character> compareOn(final String key) { 
    return new Comparator<Character>() { 
     public int compare(Character c1, Character c2) { 
      final int indexInKey1 = key.indexOf(c1); 
      final int indexInKey2 = key.indexOf(c2); 
      final int result; 
      if (indexInKey1 == -1 && indexInKey2 == -1) { 
       result = c1.compareTo(c2); //fall back to natural ordering 
      } else { 
       if (indexInKey1 == -1) { 
        result = 1; 
       } else if (indexInKey2 == -1) { 
        result = -1; 
       } else { 
        result = indexInKey1 - indexInKey2; 
       } 
      } 
      return result; 
     } 
    }; 
} 

public static void main(String[] args) { 
    final String a = "war"; 
    final String b = "Its awesome being a programmer"; 
    final List<Character> chars = new ArrayList<Character>(); 
    for (char c: b.toCharArray()) { 
     if (c != ' ') { 
      chars.add(c); 
     } 
    } 
    Collections.sort(chars, compareOn(a)); 
    System.out.println(chars); 
}