Guest User

Untitled

a guest
Apr 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import org.junit.Test;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9.  
  10. public class Ramon {
  11.  
  12. Map<Character, Integer> charWeight = new HashMap<>();
  13.  
  14. @Test
  15. public void testme() {
  16. List<String> collectionToSort = Arrays.asList("antonio", "an", "manzana", "zanahoria", "a", "man");
  17. String tosort = "zyxwvutsrqponmlkjihgfedcba";
  18. initializeMap(tosort);
  19. collectionToSort.sort(new ExtraterrestreComparator());
  20. // Si haces algun assert aqui quedaria de puta madre
  21. }
  22.  
  23. private void initializeMap(String str){
  24. for(int i = 0; i < str.length(); i++){
  25. charWeight.put(str.charAt(i), str.length() - i);
  26. }
  27. }
  28.  
  29. class ExtraterrestreComparator implements Comparator<String> {
  30. private int compareChars(Character c1, Character c2){
  31. if(charWeight.get(c1) > charWeight.get(c2)){
  32. return -1;
  33. } else {
  34. return 1;
  35. }
  36. }
  37.  
  38. @Override
  39. public int compare(String source, String target) {
  40. int idx = 0;
  41. while(idx < source.length()){
  42. if(idx >= target.length()){
  43. return -1;
  44. } else {
  45. Character sc = source.charAt(idx);
  46. Character tc = target.charAt(idx);
  47. if(!sc.equals(tc)){
  48. return compareChars(sc, tc);
  49. }
  50. }
  51. idx++;
  52. }
  53. return 0;
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment