Index: lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java =================================================================== --- lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java (revision 1466021) +++ lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java (working copy) @@ -73,6 +73,35 @@ } + /** Sorts documents in reverse order of a wrapped {@link Sorter}. */ + public static class ReverseOrderSorter extends Sorter { + + private final Sorter in; + + public ReverseOrderSorter(Sorter in) { + if (in == REVERSE_DOCS || in instanceof ReverseOrderSorter) { + throw new IllegalArgumentException("reversing the order of a reversing sorter doesn't make any sense!"); + } + this.in = in; + } + + @Override + public DocMap sort(final AtomicReader reader) throws IOException { + final int maxDoc = reader.maxDoc(); + final DocMap inMap = in.sort(reader); + return new DocMap() { + @Override + public int oldToNew(int docID) { + return maxDoc - inMap.oldToNew(docID) - 1; + } + @Override + public int newToOld(int docID) { + return maxDoc - inMap.newToOld(docID) - 1; + } + }; + } + } + /** Sorts documents in reverse order. */ public static final Sorter REVERSE_DOCS = new Sorter() { @Override @@ -90,7 +119,7 @@ }; } }; - + private static final class DocValueSorterTemplate extends SorterTemplate { private final int[] docs;