}
public Object exec(List params)
throws TemplateModelException {
if (params.size() == 0) {
throw new TemplateModelException(
"?sort_by(key) needs exactly 1 argument.");
}
String[] subvars;
Object obj = params.get(0);
if (obj instanceof TemplateScalarModel) {
subvars = new String[]{((TemplateScalarModel) obj).getAsString()};
} else if (obj instanceof TemplateSequenceModel) {
TemplateSequenceModel seq = (TemplateSequenceModel) obj;
int ln = seq.size();
subvars = new String[ln];
for (int i = 0; i < ln; i++) {
Object item = seq.get(i);
try {
subvars[i] = ((TemplateScalarModel) item)
.getAsString();
} catch (ClassCastException e) {
if (!(item instanceof TemplateScalarModel)) {
throw new TemplateModelException(
"The argument to ?sort_by(key), when it "
+ "is a sequence, must be a sequence of "
+ "strings, but the item at index " + i
+ " is not a string." );
}
}
}
} else {
throw new TemplateModelException(
"The argument to ?sort_by(key) must be a string "
+ "(the name of the subvariable), or a sequence of "
+ "strings (the \"path\" to the subvariable).");
}
return sort(seq, subvars);