Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Test;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class Ramon {
- Map<Character, Integer> charWeight = new HashMap<>();
- @Test
- public void testme() {
- List<String> collectionToSort = Arrays.asList("antonio", "an", "manzana", "zanahoria", "a", "man");
- String tosort = "zyxwvutsrqponmlkjihgfedcba";
- initializeMap(tosort);
- collectionToSort.sort(new ExtraterrestreComparator());
- // Si haces algun assert aqui quedaria de puta madre
- }
- private void initializeMap(String str){
- for(int i = 0; i < str.length(); i++){
- charWeight.put(str.charAt(i), str.length() - i);
- }
- }
- class ExtraterrestreComparator implements Comparator<String> {
- private int compareChars(Character c1, Character c2){
- if(charWeight.get(c1) > charWeight.get(c2)){
- return -1;
- } else {
- return 1;
- }
- }
- @Override
- public int compare(String source, String target) {
- int idx = 0;
- while(idx < source.length()){
- if(idx >= target.length()){
- return -1;
- } else {
- Character sc = source.charAt(idx);
- Character tc = target.charAt(idx);
- if(!sc.equals(tc)){
- return compareChars(sc, tc);
- }
- }
- idx++;
- }
- return 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment