fork(3) download
  1. import java.util.*;
  2.  
  3. class SortOrder {
  4. public static final Map<String, Integer> SORT_ORDER = initializeSortOrder();
  5.  
  6. private static Map<String, Integer> initializeSortOrder() {
  7. List<String> orderedStrings = Arrays.asList(
  8. "A",
  9. "B",
  10. "D",
  11. "C",
  12. "E",
  13. "F"
  14. );
  15. Map<String, Integer> indexByString = new HashMap<>();
  16. for (int index = 0; index < orderedStrings.size(); ++index) {
  17. indexByString.put(orderedStrings.get(index), index);
  18. }
  19. return Collections.unmodifiableMap(indexByString);
  20. }
  21.  
  22. public static void main(String[] args) {
  23. List<String> testData = new ArrayList<>(Arrays.asList(
  24. "B", "C", "D", "E", "A"
  25. ));
  26.  
  27. Collections.sort(testData, new Comparator<String>() {
  28. @Override
  29. public int compare(String s1, String s2) {
  30. return SORT_ORDER.get(s1).compareTo(SORT_ORDER.get(s2));
  31. }
  32. });
  33.  
  34. System.out.println(testData);
  35. }
  36.  
  37. }
  38.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
[A, B, D, C, E]