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 key and reverse parameters to builtin sorted function #8881

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ private static boolean hasElementWithBooleanValue(
},
useLocation = true,
useEnvironment = true)
public MutableList<?> sorted(Object self, Object key, Boolean reverse,
Location loc, Environment env)
public MutableList<?> sorted(Object self, final Object key, Boolean reverse,
final Location loc, final Environment env)
throws EvalException, InterruptedException {

ArrayList list = new ArrayList(EvalUtils.toCollection(self, loc, env));
Expand All @@ -221,41 +221,38 @@ public MutableList<?> sorted(Object self, Object key, Boolean reverse,
} else {
checkValidKeyFunc(key, loc);

final Object KEY = key;
final Location LOC = loc;
final Environment ENV = env;
final FuncallExpression AST = new FuncallExpression(Identifier.of(""), ImmutableList.of());
final FuncallExpression ast = new FuncallExpression(Identifier.of(""), ImmutableList.of());

class KeyComparator implements Comparator<Object> {
private Exception e;

public Exception getException() {
return this.e;
}
public Exception e;
Quarz0 marked this conversation as resolved.
Show resolved Hide resolved

@Override
public int compare(Object o1, Object o2) {
Quarz0 marked this conversation as resolved.
Show resolved Hide resolved
try {
return EvalUtils.SKYLARK_COMPARATOR.compare(
evalKeyFunc(o1, KEY, LOC, ENV, AST), evalKeyFunc(o2, KEY, LOC, ENV, AST));
} catch (Exception e) {
this.e = this.e == null ? e : this.e;
evalKeyFunc(o1, key, loc, env, ast),
evalKeyFunc(o2, key, loc, env, ast));
} catch (InterruptedException | EvalException e) {
if (this.e == null) {
this.e = (Exception) e;
Quarz0 marked this conversation as resolved.
Show resolved Hide resolved
}
return 0;
}
return 0;
}
}

KeyComparator comparator = new KeyComparator();
Collections.sort(list, comparator);
KeyComparator comp= new KeyComparator();
Quarz0 marked this conversation as resolved.
Show resolved Hide resolved
try {
Collections.sort(list, comp);
} catch (EvalUtils.ComparisonException e) {
throw new EvalException(loc, e);
}

Exception e = comparator.getException();
if (e != null) {
if (e instanceof InterruptedException) {
throw (InterruptedException) e;
} else if (e instanceof EvalUtils.ComparisonException) {
throw new EvalException(loc, e);
if (comp.e != null) {
if (comp.e instanceof InterruptedException) {
throw (InterruptedException) comp.e;
}
throw (EvalException) e;
throw (EvalException) comp.e;
}
}

Expand Down