Index: lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java =================================================================== --- lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java (revision 1468619) +++ lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java (working copy) @@ -546,13 +546,7 @@ // Sorts largest to smallest @Override public int compareTo(SegmentInfoAndLevel other) { - if (level < other.level) { - return 1; - } else if (level > other.level) { - return -1; - } else { - return 0; - } + return Float.compare(other.level, level); } } Index: lucene/core/src/java/org/apache/lucene/search/FieldComparator.java =================================================================== --- lucene/core/src/java/org/apache/lucene/search/FieldComparator.java (revision 1468619) +++ lucene/core/src/java/org/apache/lucene/search/FieldComparator.java (working copy) @@ -19,7 +19,6 @@ import java.io.IOException; -import org.apache.lucene.index.AtomicReader; // javadocs import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.BinaryDocValues; import org.apache.lucene.index.SortedDocValues; @@ -307,15 +306,7 @@ @Override public int compare(int slot1, int slot2) { - final double v1 = values[slot1]; - final double v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } + return Double.compare(values[slot1], values[slot2]); } @Override @@ -327,13 +318,7 @@ v2 = missingValue; } - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + return Double.compare(bottom, v2); } @Override @@ -375,13 +360,7 @@ if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Double.compare(docValue, value); } } @@ -401,17 +380,7 @@ @Override public int compare(int slot1, int slot2) { - // TODO: are there sneaky non-branch ways to compute - // sign of float? - final float v1 = values[slot1]; - final float v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } + return Float.compare(values[slot1], values[slot2]); } @Override @@ -423,14 +392,8 @@ if (docsWithField != null && v2 == 0 && !docsWithField.get(doc)) { v2 = missingValue; } - - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + + return Float.compare(bottom, v2); } @Override @@ -472,13 +435,7 @@ if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Float.compare(docValue, value); } } @@ -773,16 +730,14 @@ @Override public int compare(int slot1, int slot2) { - final float score1 = scores[slot1]; - final float score2 = scores[slot2]; - return score1 > score2 ? -1 : (score1 < score2 ? 1 : 0); + return Float.compare(scores[slot2], scores[slot1]); } @Override public int compareBottom(int doc) throws IOException { float score = scorer.score(); assert !Float.isNaN(score); - return bottom > score ? -1 : (bottom < score ? 1 : 0); + return Float.compare(score, bottom); } @Override @@ -831,15 +786,7 @@ final float value = valueObj.floatValue(); float docValue = scorer.score(); assert !Float.isNaN(docValue); - if (docValue < value) { - // reverse of FloatComparator - return 1; - } else if (docValue > value) { - // reverse of FloatComparator - return -1; - } else { - return 0; - } + return Float.compare(value, docValue); } } Index: lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java =================================================================== --- lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java (revision 1468619) +++ lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java (working copy) @@ -119,16 +119,7 @@ // Newer searchers are sort before older ones: @Override public int compareTo(SearcherTracker other) { - // Be defensive: cannot subtract since it could - // technically overflow long, though, we'd never hit - // that in practice: - if (recordTimeSec < other.recordTimeSec) { - return 1; - } else if (other.recordTimeSec < recordTimeSec) { - return -1; - } else { - return 0; - } + return Double.compare(other.recordTimeSec, recordTimeSec); } @Override Index: lucene/core/src/test/org/apache/lucene/search/TestSort.java =================================================================== --- lucene/core/src/test/org/apache/lucene/search/TestSort.java (revision 1468619) +++ lucene/core/src/test/org/apache/lucene/search/TestSort.java (working copy) @@ -964,6 +964,33 @@ dir.close(); } + /** Tests sorting on type double with +/- zero */ + public void testDoubleSignedZero() throws IOException { + Directory dir = newDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(random(), dir); + Document doc = new Document(); + doc.add(newStringField("value", "+0", Field.Store.YES)); + writer.addDocument(doc); + doc = new Document(); + doc.add(newStringField("value", "-0", Field.Store.YES)); + writer.addDocument(doc); + doc = new Document(); + IndexReader ir = writer.getReader(); + writer.close(); + + IndexSearcher searcher = newSearcher(ir); + Sort sort = new Sort(new SortField("value", SortField.Type.DOUBLE)); + + TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); + assertEquals(2, td.totalHits); + // numeric order + assertEquals("-0", searcher.doc(td.scoreDocs[0].doc).get("value")); + assertEquals("+0", searcher.doc(td.scoreDocs[1].doc).get("value")); + + ir.close(); + dir.close(); + } + /** Tests sorting on type double with a missing value */ public void testDoubleMissing() throws IOException { Directory dir = newDirectory(); Index: lucene/core/src/test/org/apache/lucene/search/TestSortDocValues.java =================================================================== --- lucene/core/src/test/org/apache/lucene/search/TestSortDocValues.java (revision 1468619) +++ lucene/core/src/test/org/apache/lucene/search/TestSortDocValues.java (working copy) @@ -604,6 +604,35 @@ dir.close(); } + /** Tests sorting on type double with +/- zero */ + public void testDoubleSignedZero() throws IOException { + Directory dir = newDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(random(), dir); + Document doc = new Document(); + doc.add(new DoubleDocValuesField("value", +0D)); + doc.add(newStringField("value", "+0", Field.Store.YES)); + writer.addDocument(doc); + doc = new Document(); + doc.add(new DoubleDocValuesField("value", -0D)); + doc.add(newStringField("value", "-0", Field.Store.YES)); + writer.addDocument(doc); + doc = new Document(); + IndexReader ir = writer.getReader(); + writer.close(); + + IndexSearcher searcher = newSearcher(ir); + Sort sort = new Sort(new SortField("value", SortField.Type.DOUBLE)); + + TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); + assertEquals(2, td.totalHits); + // numeric order + assertEquals("-0", searcher.doc(td.scoreDocs[0].doc).get("value")); + assertEquals("+0", searcher.doc(td.scoreDocs[1].doc).get("value")); + + ir.close(); + dir.close(); + } + /** Tests sorting on type double in reverse */ public void testDoubleReverse() throws IOException { Directory dir = newDirectory(); Index: lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java =================================================================== --- lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java (revision 1468619) +++ lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java (working copy) @@ -139,28 +139,13 @@ @Override public int compare(int slot1, int slot2) { - final double v1 = values[slot1]; - final double v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } + return Double.compare(values[slot1], values[slot2]); } @Override public int compareBottom(int doc) { - final double v2 = docVals.doubleVal(doc); - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + return Double.compare(bottom, docVals.doubleVal(doc)); } @Override @@ -188,13 +173,7 @@ public int compareDocToValue(int doc, Double valueObj) { final double value = valueObj; final double docValue = docVals.doubleVal(doc); - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Double.compare(docValue, value); } } } Index: solr/core/src/test/org/apache/solr/schema/DocValuesTest.java =================================================================== --- solr/core/src/test/org/apache/solr/schema/DocValuesTest.java (revision 1468619) +++ solr/core/src/test/org/apache/solr/schema/DocValuesTest.java (working copy) @@ -122,6 +122,26 @@ assertQ(req("q", "*:*", "sort", "stringdv asc", "rows", "1", "fl", "id"), "//str[@name='id'][.='2']"); } + + public void testDocValuesSorting2() { + assertU(adoc("id", "1", "doubledv", "12")); + assertU(adoc("id", "2", "doubledv", "50.567")); + assertU(adoc("id", "3", "doubledv", "+0")); + assertU(adoc("id", "4", "doubledv", "4.9E-324")); + assertU(adoc("id", "5", "doubledv", "-0")); + assertU(adoc("id", "6", "doubledv", "-5.123")); + assertU(adoc("id", "7", "doubledv", "1.7976931348623157E308")); + assertU(commit()); + assertQ(req("fl", "id", "q", "*:*", "sort", "doubledv asc"), + "//result/doc[1]/str[@name='id'][.='6']", + "//result/doc[2]/str[@name='id'][.='5']", + "//result/doc[3]/str[@name='id'][.='3']", + "//result/doc[4]/str[@name='id'][.='4']", + "//result/doc[5]/str[@name='id'][.='1']", + "//result/doc[6]/str[@name='id'][.='2']", + "//result/doc[7]/str[@name='id'][.='7']" + ); + } public void testDocValuesFaceting() { for (int i = 0; i < 50; ++i) {