Skip to content

Commit

Permalink
Fix sorting indices of a list with children (#6897)
Browse files Browse the repository at this point in the history
  • Loading branch information
potarodev authored Jul 27, 2024
1 parent 7b2dcd9 commit 22a2bfb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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
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"

0 comments on commit 22a2bfb

Please sign in to comment.