From: Reino Ruusu Date: Mon, 4 Mar 2019 15:33:03 +0000 (+0200) Subject: Resolve tie situations in n lowest/highest element selection. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F46%2F2746%2F3;p=simantics%2Fdistrict.git 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 --- 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; } }