0

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"

6
  • Your sample list contains three words whose first letter is the same as its last letter, namely bob, poop and zpppz. According to your criteria, the order you are getting is the correct order. Commented May 23, 2020 at 4:46
  • But I need the expected output where the words which fails the criteria should not be disturbed from their place Commented May 23, 2020 at 4:48
  • According to your criteria, the sorted list should start with all the words whose first and last letter is the same. Your expected output, as stated in your question does not contain that order. Why should zllo come before zpppz in your expected output? Am I missing something? is z the same letter as o ? Commented May 23, 2020 at 4:54
  • zllo fails the criteria so it should be left undisturbed in its position and the three words which meets the criteria should be sorted and should be placed in place . It's not like the words which meets the criteria should be placed at beginning bluntly Commented May 23, 2020 at 4:58
  • So if your unsorted list was zpppz, zllo, poop, yea, bob then your sorted list would be bob, zllo, poop, yea, zpppz, correct? Commented May 23, 2020 at 5:09

2 Answers 2

2

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]

Sign up to request clarification or add additional context in comments.

3 Comments

Exactly. Method sort() in class java.util.Arrays uses merge sort which is not a suitable sort algorithm for how the OP wants to sort his array.
This is Selection Sort mate! Btw thanks for the response.
@dineshbabu you are right, well, at first, the intention with some kind of bubble sort lol. Thanks
1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.