Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorting indices of a list with children #6897

Merged
merged 9 commits into from
Jul 27, 2024
9 changes: 8 additions & 1 deletion src/main/java/ch/njol/skript/expressions/ExprIndices.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import ch.njol.util.Pair;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -101,8 +102,14 @@ protected String[] get(Event e) {
if (sort) {
int direction = descending ? -1 : 1;
return variable.entrySet().stream()
.map((entry) -> new Pair<>(
entry.getKey(),
entry.getValue() instanceof Map<?,?>
? ((Map<?,?>) entry.getValue()).get(null)
: entry.getValue()
))
.sorted((a, b) -> ExprSortedList.compare(a.getValue(), b.getValue()) * direction)
.map(Entry::getKey)
.map(Pair::getKey)
.toArray(String[]::new);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test "sorted indices with children":
set {_test::1} to 111
set {_test::2} to 555
set {_test::3} to 444
set {_test::1::a} to 2
set {_test::2::b} to 3
set {_test::3::c} to 6
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
set {_test::3::a::foo} to "i"
set {_test::3::b::bar} to "love"
set {_test::3::c::baz} to "skript"
set {_indices::*} to (sorted indices of {_test::*} in ascending order)

assert {_indices::*} is ("1", "3", "2") with "sorted indices on list with children threw or was incorrect"
assert {_test::*} is (111, 555, 444) with "modified children wrongly"
assert {_test::3::*} is 6 with "modified children wrongly"