0

I have a string array in Java. I need to sort it by the number of character "a"s within the word (decreasing order). If some words contain same number of character a, then I need to sort those words by their lengths(decreasing order). And if the length is same, then alphabetically.

Example array:

["aaaasd","a","aab","aaaabcd","ef","cssssssd","fdz" ,"kf","zc","lklklklklklk","l"]

needs to be sorted like:

["aaaabcd","aaaasd","aab","a","lklklklklklk","cssss ssd","fdz","ef","kf","zc","l"]

3
  • 1
    did you try something? Commented Dec 10, 2015 at 20:34
  • yes I was thinking to loop through the array to get the numbers of "a"s on the words. but I am newbie in Java and I don't know if this is a good way to do it. Plus even if it is a good way to start, I have no idea what to do on the next step for the 2. and 3. criteria. So stuck in here. Commented Dec 10, 2015 at 20:46
  • shmosel yes. Version 8 Update 40 Commented Dec 10, 2015 at 20:57

3 Answers 3

2

As it's already mentioned, just use Arrays.sort() method with your own Comparator:

Arrays.sort(arr, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        if (countA(o1) == countA(o2)) {
            if (o1.length() == o2.length()) {
                // Compare lexicographically
                return o2.compareTo(o1);
            }
            // Compare by the length
            return o2.length() - o1.length();
        }
        // Compare by the number of 'a'
        return countA(o2) - countA(o1);
    }

    private int countA(String s) {
        int total = 0;
        for (int pos = 0; pos < s.length(); ++pos) {
            if (s.charAt(pos) == 'a') {
                total++;
            }
        }
        return total;
    }
});

Where arr is your String array.

The main idea is to check criterias according to their precedence and check new criteria only if objects are equal according to all previous criterias.

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

2 Comments

Thanks very much for your help. I would never get the idea of comparator in reverse order with ifs by myself. I am a Java newbie and had no idea about this usage at all.
@CeyhunGanioglu No problem ;) All such comparators are written in the same manner and depend on programming language only in details.
2

This should do it:

// compare count of 'a' chars, descending
Comparator<String> comparator = Comparator.comparing(s -> s.chars().filter(c -> c == 'a').count(), Comparator.reverseOrder());

// then string length, descending
comparator = comparator.thenComparing(String::length, Comparator.reverseOrder());

// then natural (alphabetical) order
comparator = comparator.thenComparing(Comparator.naturalOrder());

// apply sort
Arrays.sort(strArr, comparator);

Comments

1

You could use a util method in Arrays class:

public static <T> void sort(T[] a, Comparator<? super T> c) 

Just write your own implementation of Comparator and pass it as parameter along with the array of strings you wish to sort by it.

1 Comment

I've already had a look at the comparator examples on the internet but gave me no idea about solving this kind of problem with 3 criterias.

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.