From 50306ed44f39142ec59265c7329e8e5930b9ff38 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Mon, 4 Mar 2019 17:33:03 +0200 Subject: [PATCH] Resolve tie situations in n lowest/highest element selection. Pick all elements that match the value of the n'th lowest/highest one. Change-Id: I066ae39a2f7db73db93a6c3d4c9531ba4300236f --- .../district/selection/ElementSelector.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/org.simantics.district.selection/src/org/simantics/district/selection/ElementSelector.java b/org.simantics.district.selection/src/org/simantics/district/selection/ElementSelector.java index 74eaa227..bf051b4c 100644 --- a/org.simantics.district.selection/src/org/simantics/district/selection/ElementSelector.java +++ b/org.simantics.district.selection/src/org/simantics/district/selection/ElementSelector.java @@ -543,13 +543,12 @@ public class ElementSelector { @SuppressWarnings("unchecked") @Override Collection select(ReadGraph graph, Collection elements) { - List result = new ArrayList<>(elements); List result2 = Lists.map(new FunctionImpl1() { @Override public Tuple2 apply(Resource r) { return new Tuple2(r, getPropertyValue(graph, r, propertyName)); } - }, result); + }, new ArrayList<>(elements)); result2 = Lists.filter(new FunctionImpl1() { @Override @@ -560,17 +559,21 @@ public class ElementSelector { result2.sort((t1, t2) -> smallest ? Double.compare((Double) t1.c1, (Double) t2.c1) : Double.compare((Double) t2.c1, (Double) t1.c1)); - if (resultCount < result2.size()) - result2 = result2.subList(0, resultCount); + if (resultCount < result2.size()) { + double limitValue = (double) result2.get(resultCount-1).c1; + + // Expand selection to contain all items with the same value as the nth one + int count = resultCount; + while (count < result2.size() && (double)result2.get(count).c1 == limitValue) count++; + result2 = result2.subList(0, count); + } - result = Lists.map(new FunctionImpl1() { + return (List) Lists.map(new FunctionImpl1() { @Override public Resource apply(Tuple2 p0) { return (Resource) p0.c0; } }, result2); - - return result; } } -- 2.45.1