I'm having trouble reversing a LinkedList. In other words, I need them sorted in za order (in contrast to az). I've tried Collections.reverse but is not coming into effect? I have the following:
import java.io.*;
import java.util.*;
public class pa9Driver {
//create two list
//1st List is of type word class
//2nd list is of type Anagram_Family
public static List<Word> words = new LinkedList<Word>();
public static List<AnagramFamily> familyList = new LinkedList<AnagramFamily>();
//a main method for driver class
public static void main(String[] args) {
//call the generate method to read word from the file
generate_WordList();
//sort the word list
Collections.sort(words);
//generate the anagram family for the word
generate_FamilyList();
//sort the anagram family list
Collections.sort(familyList, new anagramFamilyComparator());
//reverse the anagram family list
Collections.reverse(familyList);
topFive();
}//main ends
public static void topFive() {
int i;
for(i = 0; i < 15; i++) {
System.out.print(familyList.get(i).getCanonicalForm1() + ", ");
System.out.print(familyList.get(i).getSize() + ": ");
System.out.println(familyList.get(i));
}
}
//method that read word
public static void generate_WordList() {
File inFile12=new File("words.txt");
Scanner fileRead1=null;
try {
fileRead1 = new Scanner(inFile12);
} catch (Exception exe) {
exe.printStackTrace();
System.exit(0);
}
//until the file has words read the words
while(fileRead1.hasNext()) {
words.add(new Word(fileRead1.next()));
}
}
//generate the anagram and add it to the current family
public static void generate_FamilyList() {
Iterator<Word> readWord1 = words.iterator();
Word previousWord1 = words.get(0);
familyList.add(new AnagramFamily());
int index1 = 0;
while(readWord1.hasNext()) {
Word currentWord1 = readWord1.next();
if(currentWord1.getCanonicalForm1().equals(previousWord1
.getCanonicalForm1())) {
familyList.get(index1).add(currentWord1);
} else {
index1++;
familyList.add(new AnagramFamily());
familyList.get(index1).add(currentWord1);
}
previousWord1 = currentWord1;
}
}
}
For convenience sake, I'll only show the first few lines of code that I have and that is expected. Currently:
[apers, apres, asper, pares, parse, pears, prase, presa, rapes, reaps, spare, spear]
[alerts, alters, artels, estral, laster, ratels, salter, slater, staler, stelar, talers]
Expected:
[spear, spare, reaps, rapes, presa, prase, pears, parse, pares, asper, apres, apers]
[talers, stelar, staler, slater, salter, ratels, laster, estral, artels, alters, alerts]
Try:
Collections.sort(familyList, Comparator.reverseOrder());
Alternatively, you can do:
Collections.sort(familyList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
//Print list in reverse order
for(String st : familyList){
System.out.println(st);
}
Seeing your code, it seems AnagramFamily
is a kind of List
, which you are not sorting.
You need to sort the AnagramFamily (List of String) with a StringComparator
for getting your desired output.
I think you can use compareTo for this action no ? compareTo from Comparable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.