Synergy76 Synergy76 - 7 months ago 8
Java Question

How can I add a method this program so it sorts the output from highest to lowest frequency?

I'm somewhat lost in regards to how to go about it, I know I need a sorting method of some sorts as you can see in my commented code, but i'm confused on what variables (reference and primitive) should be placed where it regards to constructing the method. I got lost a bit in my own code.
Below is the code:

class Big
{
private static String ALLCHARS = "abcdefghijklmnopqrstuvwxyz";
private static String[] SortedArray = new String[26];

public static void main(String[] args)
{
LetterCount[] histogram = new LetterCount[26];
// Array of LetterCount Objects.
arrayInitializer(histogram);
//arraySorter(histogram);
String input = args[0].toLowerCase();

for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (ALLCHARS.indexOf(c) == -1) {
continue;
}
int posOfC = ALLCHARS.indexOf(c);
/*System.out.println(c + ": " + posOfC);
System.out.println(histogram[posOfC]);
We got rid of these print statements because were using the asString method
*/
// Any one element in the histogram[] is a letter count object.
histogram[posOfC].count++;
//Only objects can be null, posOfC is an int.
//histogram is a null pointer in this current code.
}

for (int i = 0; i < histogram.length; i++) {
System.out.println(histogram[i].asString());
}
}
private static void arrayInitializer(LetterCount[] input) {
for (int i = 0; i < input.length; i++) {
LetterCount lettcount = new LetterCount();
lettcount.count = 0;
lettcount.letter = ALLCHARS.charAt(i);
input[i] = lettcount;
}
}
private static void arraySorter (LetterCount[] input, String[] ToBeSorted) {
// for (int i = 0; i < input.length; i++)
// if (input[i] < ToBeSorted[i])
// swap(input[i], ToBeSorted[i]);

}
// private static void swapMethod (LetterCount[] input1, String[] input2) {
// int temporarySwapVariable = LetterCount[input1];
// String[input2] = temporarySwapVariable;
// LetterCount[input1] = String[input2];
//}
}
//
class LetterCount {
char letter;
int count;

public String asString() {
return letter + ":" + count;
}
}

Answer

The best way is to make your LetterCount class a Comparable, so you can just use library functions to sort it easily. Change LetterCount to this:

class LetterCount implements Comparable {

    char letter;
    int count;

    public String asString() {
        return letter + ":" + count;
    }

    @Override
    public int compareTo(Object t) {
        return ((LetterCount) t).count - count;
    }
}

And then all you need to do is add the code to sort the array right before you print it out:

    Arrays.sort(histogram);

    for (int i = 0; i < histogram.length; i++) {
        System.out.println(histogram[i].asString());
    }

You will also need to import java.util.Arrays;

Explanation:

Comparable is an interface that you can use to tell java how to compare one instance of your class to another. That lets java sort the array for you (it makes sense that java can't sort it unless it knows how to compare two elements). The way this works is anything that implements Comparable must override the method compareTo and specify how to compare that object to another object of its type.

Edit:

To not print characters with count 0, just add an if statement to your print method:

for (int i = 0; i < histogram.length; i++) {
  if (histogram[i].count != 0) {
    System.out.println(histogram[i].asString());
  }
}