2

I am trying to sort one character array char[] input based on another character's array frequency of each character char[] freq

A extremely simple example would be:

input: bey
freq input: bbbyye

output: after sorted would be "bye"

I am iterating over my freq input and putting a key value pair like the following question: char c is key; an int is the value.

Now; how do I sort input based on the frequency? My attempts to implement Comparator have met with failure. My code is now identical to the following accepted answer.

What I "want" to do is bad java or pseudo-code is:

char[] input;
Arrays.sort(input, new Comparator<Character>() {
@Override
public int compare(Character o1, Character o2) {
  return counts.get(o2) - counts.get(o1); //counts is a static
  //global hashmap with the frequency values
 }
});

I do not expect that to work and it does not the error is The method sort(char[]) in the type Arrays is not applicable for the arguments (char[], new Comparator(){})

Community
  • 1
  • 1

2 Answers2

5

I am confused, it appears that your initial approach does work. Here is a small example of it:

import java.util.*;

public class TestArrayComparator {
   public static Map<Character, Integer> freq = new HashMap<>();
   static {
      freq.put('b',3);
      freq.put('y',2);
      freq.put('e',1);
   }
   public static void main(String[] args) {
      Character[] input = new Character[]{'b','e','y'};

      Arrays.sort(input, new Comparator<Character>() {
         @Override
         public int compare(Character o1, Character o2) {
            return freq.get(o2) - freq.get(o1);
         }
      });
      System.out.println(Arrays.toString(input));
   }
}
//outputs ['b','y','e']

I am not saying this is the best approach, but it does work.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • I figured out the problem, rather than doing Character[] input I've been doing char input []. Do you now if there s there a difference in C# - I don't recall running into this before. As for the "best" approach would you think tmanion's is better? –  Jun 10 '14 at 18:33
  • It really depends on what you are using this for, if it's for production code @tmanion's solution is more encapsulated and testable. If you are just playing around, this one is probably fine. I don't know much about type conversions in C#, so I am no help there. – Hunter McMillen Jun 10 '14 at 18:46
  • Thanks; for the response - if it isn't too much to ask how do you convert from the primitive type charArray to a Character type in java? I'm coming from the C/C# world. –  Jun 10 '14 at 18:49
  • @xphill64x Unfortunately, there are no built-in ways to do this in Java(that I know of). You will have to do it by hand with a new array of type Character of the same size and iterate over your primitive char array then adding those to the Character array. – Hunter McMillen Jun 10 '14 at 18:56
1

I think what you should do in this scenario is make a new class for a FrequencyCharacter

public class FrequencyCharacter implements Comparable<FrequencyCharacter> {
    private int occurrences;
    private char c;

    public FrequencyCharacter(int occurrences, char c) {
        this.occurrences = occurrences;
        this.c = c;
    }

    @Override
    public int compareTo(FrequencyCharacter o) {
        return Integer.compare(occurrences, o.occurrences);
    }

}

Create these objects then sort these. You can easily print your result then.

tmanion
  • 401
  • 2
  • 11