From 650040d134c5ef99a20b2a5dab23b48c328ecf53 Mon Sep 17 00:00:00 2001 From: Ian Swepston Date: Fri, 13 Jan 2023 15:53:50 -0600 Subject: [PATCH] Sorts facet stat responses by sum, agnostic to datatype. Sorted largest to smallest. --- .../io/zulia/server/search/StatCombiner.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/zulia-server/src/main/java/io/zulia/server/search/StatCombiner.java b/zulia-server/src/main/java/io/zulia/server/search/StatCombiner.java index ff408e8d..39274161 100644 --- a/zulia-server/src/main/java/io/zulia/server/search/StatCombiner.java +++ b/zulia-server/src/main/java/io/zulia/server/search/StatCombiner.java @@ -101,7 +101,30 @@ public ZuliaQuery.StatGroup getCombinedStatGroupAndConvertToExternalType() { // Generate return message return StatGroup.newBuilder().setStatRequest(this.statRequest).setGlobalStats(globalStats) - .addAllFacetStats(facetStats.stream().map(FacetStats.Builder::build).toList()).build(); + .addAllFacetStats(facetStats.stream().map(FacetStats.Builder::build).sorted(this::reverseCompareFacetStats).toList()).build(); + } + + /** + * Comparator method for complex container type Facet Stats. Sorts by sum, if it exists and largest to smallest, rather than natural order + * @param o1 Any facet stat value + * @param o2 Any other facet stat value + * @return Result of [Type].compare for actively populated datatype + */ + private int reverseCompareFacetStats(FacetStats o1, FacetStats o2) { + if (o1.hasSum() && o2.hasSum()) { + // Reversing the arguments reverses the comparison by the comparators + SortValue sv1 = o2.getSum(); + SortValue sv2 = o1.getSum(); + // Compare each type until one is not equal. If all are equal, values are equal + int comp = Double.compare(sv1.getDoubleValue(), sv2.getDoubleValue()); + if (comp == 0) comp = Integer.compare(sv1.getIntegerValue(), sv2.getIntegerValue()); + if (comp == 0) comp = Long.compare(sv1.getLongValue(), sv2.getLongValue()); + if (comp == 0) comp = Float.compare(sv1.getFloatValue(), sv2.getFloatValue()); + if (comp == 0) comp = Long.compare(sv1.getDateValue(), sv2.getDateValue()); + return comp; + } else { + return 0; + } } /**