Skip to content

Commit

Permalink
Add explicit sort order to EffSort (#6997)
Browse files Browse the repository at this point in the history
* Add optional ascending, descending sort order to SortEff

* Add version to Since annotation for explicit sort order

* Change toString() to reflect change in syntax

* Add missing space in EffSort#toString

* Change "explicit order" to "sort order" for clarity

* Revert "Change "explicit order" to "sort order" for clarity"

This reverts commit ae11d23.

* Update src/main/java/ch/njol/skript/effects/EffSort.java

---------

Co-authored-by: Moderocky <admin@moderocky.com>
  • Loading branch information
TenFont and Moderocky authored Aug 24, 2024
1 parent a4c107b commit 6b40d2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 10 additions & 5 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 (sort order)")
@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 Expand Up @@ -160,7 +164,8 @@ public boolean hasIndices() {

@Override
public String toString(@Nullable Event event, boolean debug) {
return "sort" + unsortedObjects.toString(event, debug)
return "sort " + unsortedObjects.toString(event, debug)
+ " in " + (descendingOrder ? "descending" : "ascending") + " order"
+ (mappingExpr == null ? "" : " by " + mappingExpr.toString(event, debug));
}

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

0 comments on commit 6b40d2e

Please sign in to comment.