fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5.  
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args)
  11. {
  12. String[] order = {"d", "c"};
  13. String[] input = {"c","b","a","d", "e"};
  14.  
  15. //List<String> inputList = new ArrayList<String>(input);
  16.  
  17. List<String> l = new ArrayList<String>();
  18. for (int i = 0; i < input.length; i++)
  19. l.add(input[i]);
  20.  
  21. Collections.sort(l, new CustomComparator(order));
  22.  
  23. for (String s: l){
  24. System.out.print(s);
  25. }
  26. }
  27.  
  28.  
  29.  
  30. public static class CustomComparator implements Comparator<String> {
  31. Map<String, Integer> order;
  32.  
  33. public CustomComparator(String[] orderArray){
  34. order = new HashMap<String, Integer>();
  35.  
  36. for(int i = 0; i< orderArray.length; i++){
  37. order.put(orderArray[i], i);
  38. }
  39. }
  40.  
  41.  
  42. public int compare(String a, String b){
  43. if (order.containsKey(a) && order.containsKey(b)){
  44. return order.get(a) - order.get(b);
  45. }
  46. if (order.containsKey(a)){
  47. return -1;
  48. }
  49. if (order.containsKey(b)){
  50. return 1;
  51. }
  52. return a.compareTo(b);
  53. }
  54. }
  55. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
dcabe