简体   繁体   中英

Java Custom Comparator for sorting array of strings

Here im trying to sort the words in a string that has starting and ending letter as same. it should be sorted in place, other words are left undisturbed.

import java.util.*;
public class MyClass {
public static void main(String args[]) {
  String arr[]={"zpppz","poop","zllo","bob","yea"};
  Arrays.sort(arr , new Comparator<String>(){
      public int compare(String a ,String b){
          if((a.charAt(0) == a.charAt(a.length()-1 ) )&&(b.charAt(0)==b.charAt(b.length()-1 ) )  ){
           return a.compareTo(b);
          }
          return 0;
      }
  } );
  for(String s: arr ) System.out.println(s);
}
} 

expected output: "bob" "poop" "zllo" "zpppz" "yea" but im getting output as: "bob" "poop" "zpppz" "zllo" "yea"

What about using your Comparator with something like Selection Sort?

public class Test {
    public static void main(String args[]) {
        String arr[]={"zpppz","poop","zllo","bob","yea"};
        Comparator<String> comparator = new Comparator<>() {
            public int compare(String a, String b) {
                if ((a.charAt(0) == a.charAt(a.length() - 1)) && (b.charAt(0) == b.charAt(b.length() - 1))) {
                    return a.compareTo(b);
                }
                return 0;
            }
        };
        selectionSort(arr, comparator);
    }

    static <T> void selectionSort(T[] a, Comparator<T> c) {
        for (int i = 0; i < a.length; i++) {
            for (int j = i; j < a.length; j++) {
                if (c.compare(a[i], a[j]) > 0) {
                    T hold = a[i];
                    a[i] = a[j];
                    a[j] = hold;
                }
            }
        }
    }
}

result: [bob, poop, zllo, zpppz, yea]

The following code does what you want but does not use Comparator .
If it is not acceptable then let me know and I will delete this answer.

private static boolean isCandidate(String word) {
    boolean candidate = false;
    if (word != null && word.length() > 0) {
        candidate = word.charAt(0) == word.charAt(word.length() - 1);
    }
    return candidate;
}
/**********************************************************/
String arr[] = {"zpppz", "poop", "zllo", "bob", "yea"};
List<Integer> indexList = new ArrayList<>();
List<String> words2sort = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
    if (isCandidate(arr[i])) {
        indexList.add(Integer.valueOf(i));
        words2sort.add(arr[i]);
    }
}
if (words2sort.size() > 0) {
    Collections.sort(words2sort);
    int index = 0;
    String[] sorted = new String[arr.length];
    for (int i = 0; i < arr.length; i++) {
        if (index < indexList.size() && indexList.get(index).intValue() == i) {
            sorted[i] = words2sort.get(index);
            index++;
        }
        else {
            sorted[i] = arr[i];
        }
    }
    System.out.println(Arrays.asList(sorted));
}

Result of running above code:

[bob, poop, zllo, zpppz, yea]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM