Reputation: 147
So we have this problem that we are trying to figure out. Heres what the problem asks. Lester has a list of words that he wants to print, not in the usual alphabetical order, but not in random order either. He decided on a method to sort the words that he calls AlphaFun order. The AlphaFun order method sorts the words using the following procedure 1.first compare the 2nd letter of the words 2.then compare teh 4th letter of the words(the 4th letter will be considered to be a space in words containing less than 4 letters). 3.compare the last letter of the words (the last letter will always be the last letter of the word, not a space). 4.finally compare the first letter in the words. 5.if all of the above are the same characters, then the words used for those letters are sorted alphabetically.
note these examples Words:
EGG
EGGS
BREAD
ALPHAFUN:
G SE
GSSE
RADB
Input the input files contains and unkown number of lines, where each line of inputer contains a single word consisting of 3 to 10 letters. output you will print the words in alpha fun order
example input file: BREAD AERIE BROAD EGGS EGG WALLET
example output to screen WALLET AERIE EGG EGGS BREAD BROAD
how could we do this problem? we have been stuck on it for like 2 hours.
Upvotes: 0
Views: 140
Reputation: 1500155
how could we do this problem? we have been stuck on it for like 2 hours.
Java makes this pretty easy, actually. You just need to implement Comparator<String>
in a class, e.g.
public class AlphaFunComparator implements Comparator<String> {
public int compare(String o1, String o2) {
// Implement rules described in the question
}
}
Then you just need to load the file into a list, and call:
Collections.sort(list, new AlphaFunComparator());
Print out the list, and you're done.
Upvotes: 1
Reputation: 30089
Implement your ordering logic in a Comparator class, then collect your Strings into a List of some sort (ArrayList), finally use Collections.sort(arrayList, myComparator):
Collections.sort(List, Comparator)
Upvotes: 1