Skip to content

Commit

Permalink
Fix Sorted List Expression (#6102)
Browse files Browse the repository at this point in the history
* Fix ExprSortedList

* Update src/main/java/ch/njol/skript/expressions/ExprSortedList.java

Co-authored-by: LimeGlass <[email protected]>

* Fix test

Co-authored-by: Moderocky <[email protected]>

---------

Co-authored-by: LimeGlass <[email protected]>
Co-authored-by: Moderocky <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent 5ba4dba commit 8895157
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/main/java/ch/njol/skript/expressions/ExprSortedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;

import java.lang.reflect.Array;
import java.util.Arrays;

@Name("Sorted List")
@Description({"Sorts given list in natural order. All objects in list must be comparable;",
"if they're not, this expression will return nothing."
})
@Examples({"set {_sorted::*} to sorted {_players::*}"})
@Description("Sorts given list in natural order. All objects in list must be comparable; if they're not, this expression will return nothing.")
@Examples("set {_sorted::*} to sorted {_players::*}")
@Since("2.2-dev19")
public class ExprSortedList extends SimpleExpression<Object> {

static{
static {
Skript.registerExpression(ExprSortedList.class, Object.class, ExpressionType.COMBINED, "sorted %objects%");
}

Expand All @@ -67,29 +66,26 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
@Nullable
protected Object[] get(Event e) {
Object[] unsorted = list.getArray(e);
Object[] sorted = (Object[]) Array.newInstance(getReturnType(), unsorted.length); // Not yet sorted...

for (int i = 0; i < sorted.length; i++) {
Object value = unsorted[i];
if (value instanceof Long) {
// Hope it fits to the double...
sorted[i] = (double) (Long) value;
} else {
// No conversion needed
sorted[i] = value;
}
}

protected Object[] get(Event event) {
try {
Arrays.sort(sorted); // Now sorted
} catch (IllegalArgumentException | ClassCastException ex) { // In case elements are not comparable
return new Object[]{}; // We don't have a sorted array available
return list.stream(event)
.sorted(ExprSortedList::compare)
.toArray();
} catch (IllegalArgumentException | ClassCastException e) {
return (Object[]) Array.newInstance(getReturnType(), 0);
}
return sorted;
}

@SuppressWarnings("unchecked")
private static <A, B> int compare(A a, B b) throws IllegalArgumentException, ClassCastException {
Comparator<A, B> comparator = Comparators.getComparator((Class<A>) a.getClass(), (Class<B>) b.getClass());
if (comparator != null && comparator.supportsOrdering())
return comparator.compare(a, b).getRelation();
if (!(a instanceof Comparable))
throw new IllegalArgumentException();
return ((Comparable<B>) a).compareTo(b);
}

@Override
@Nullable
@SuppressWarnings("unchecked")
Expand Down
17 changes: 17 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprSortedList.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "sort":
# Populate list
set {_i} to 0
loop 5 times:
set {_i} to {_i} + 1
set {_list::%{_i}%} to a random integer from 1 to 100
loop 5 times:
set {_i} to {_i} + 1
set {_list::%{_i}%} to a random number from 1 to 100

# Test sorting
loop sorted {_list::*}:
if {_prev} is set:
assert loop-value >= {_prev} with "Couldn't sort correctly"
set {_prev} to loop-value

assert (sorted 1 and "test") is not set with "Sorting incomparable values returned a value"

0 comments on commit 8895157

Please sign in to comment.