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 Sorted List Expression #6102

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"