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

Add explicit sort order to EffSort #6997

Merged
merged 8 commits into from
Aug 24, 2024
12 changes: 8 additions & 4 deletions src/main/java/ch/njol/skript/effects/EffSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@
"set {_words::*} to \"pineapple\", \"banana\", \"yoghurt\", and \"apple\"",
"sort {_words::*} # alphabetical sort",
"sort {_words::*} by length of input # shortest to longest",
"sort {_words::*} in descending order by length of input # longest to shortest",
"sort {_words::*} based on {tastiness::%input%} # sort based on custom value"
})
@Since("2.9.0")
@Since("2.9.0, INSERT VERSION (explicit order)")
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
@Keywords("input")
public class EffSort extends Effect implements InputSource {

static {
Skript.registerEffect(EffSort.class, "sort %~objects% [(by|based on) <.+>]");
Skript.registerEffect(EffSort.class, "sort %~objects% [in (:descending|ascending) order] [(by|based on) <.+>]");
if (!ParserInstance.isRegistered(InputData.class))
ParserInstance.registerData(InputData.class, InputData::new);
}
Expand All @@ -73,6 +74,7 @@ public class EffSort extends Effect implements InputSource {
@Nullable
private String unparsedExpression;
private Variable<?> unsortedObjects;
private boolean descendingOrder;

private Set<ExprInput<?>> dependentInputs = new HashSet<>();

Expand All @@ -88,6 +90,7 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
return false;
}
unsortedObjects = (Variable<?>) expressions[0];
descendingOrder = parseResult.hasTag("descending");

if (!parseResult.regexes.isEmpty()) {
unparsedExpression = parseResult.regexes.get(0).group();
Expand All @@ -106,10 +109,11 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
@Override
protected void execute(Event event) {
Object[] sorted;
int sortingMultiplier = descendingOrder ? -1 : 1;
if (mappingExpr == null) {
try {
sorted = unsortedObjects.stream(event)
.sorted(ExprSortedList::compare)
.sorted((o1, o2) -> ExprSortedList.compare(o1, o2) * sortingMultiplier)
.toArray();
} catch (IllegalArgumentException | ClassCastException e) {
return;
Expand All @@ -127,7 +131,7 @@ protected void execute(Event event) {
}
try {
sorted = valueToMappedValue.entrySet().stream()
.sorted(Map.Entry.comparingByValue(ExprSortedList::compare))
.sorted(Map.Entry.comparingByValue((o1, o2) -> ExprSortedList.compare(o1, o2) * sortingMultiplier))
.map(Map.Entry::getKey)
.toArray();
} catch (IllegalArgumentException | ClassCastException e) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffSort.sk
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ test "sorting":
sort {_numbers::*}
assert {_numbers::*} is integers from 1 to 50 with "improper sorting of numbers"

set {_numbers::*} to shuffled integers from 1 to 50
sort {_numbers::*} in descending order
assert {_numbers::*} is integers from 50 to 1 with "improper sorting of numbers in descending order"

set {_numbers::*} to shuffled integers from 1 to 5
sort {_numbers::*} by input * 20 + 4 - 3 # linear transformations don't affect order
assert {_numbers::*} is integers from 1 to 5 with "improper custom sorting of numbers"
Expand Down