I am trying to sort ArrayList objects from PostingsEntry according to the score attribute of PostingsEntry objects. The list is in the PostingsList object, which has a sort() method.
public class PostingsEntry implements Comparable<PostingsEntry>{ public int docID; public double score = 0; private TreeSet<Integer> positions = new TreeSet<Integer>(); public int compareTo( PostingsEntry other ) { return Double.compare( other.score, score ); } } public class PostingsList{ private int position = 0; private ArrayList<PostingsEntry> list = new ArrayList<PostingsEntry>(); private class PostingsEntryComparator implements Comparator<PostingsEntry>{ @Override public int compare(PostingsEntry pA, PostingsEntry pB){ return pA.docID - pB.docID; } } public int size() { return list.size(); } public PostingsEntry get( int i ) { return list.get( i ); } public void sort(){ Collections.sort(list, new PostingsEntryComparator()); } }
I am trying to sort the list here:
And I print the results:
for(int i=0; i<postingsList.size(); i++){ System.out.println(index.docNames.get(postingsList.get(i).docID)); System.out.printf("score: %f\n\n", postingsList.get(i).score); }
But I get:
davisWiki/Zombie_Attack_Response_Guide.f score: 0,019064 davisWiki/EvanGray.f score: 0,004368 davisWiki/Mortal_Forever.f score: 0,002708 davisWiki/JasonRifkind.f score: 0,767518 davisWiki/Measure_Z.f score: 0,031980
Which shows that the list is clearly not sorted. Where am I mistaken?
java
Sahand
source share