Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mcmonkey4eva/Denizen-Core
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 10, 2016
2 parents ae6fe1d + f2487de commit cb9b7a0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/main/java/net/aufdemrand/denizencore/objects/dList.java
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,48 @@ public int compare(String o1, String o2) {
}
});

// <--[tag]
// @attribute <li@list.sort_by_number[<tag>]>
// @returns dList
// @description
// returns a copy of the listed, sorted such that the lower numbers appear first, and the higher numbers appear last.
// Rather than sorting based on the item itself, it sorts based on a tag attribute read from within the object being read.
// For example, you might sort a list of players based on the amount of money they have, via .sort_by_number[money] on the list of valid players.
// -->

registerTag("sort_by_number", new TagRunnable() {
@Override
public String run(final Attribute attribute, final dObject object) {
dList newlist = new dList((dList) object);
try {
Collections.sort(newlist, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
double r1 = new Element(ObjectFetcher.pickObjectFor(o1).getAttribute(new Attribute(attribute.getContext(1),
attribute.getScriptEntry(), attribute.context))).asDouble();
double r2 = new Element(ObjectFetcher.pickObjectFor(o2).getAttribute(new Attribute(attribute.getContext(1),
attribute.getScriptEntry(), attribute.context))).asDouble();
double value = r1 - r2;
if (value == 0) {
return 0;
}
else if (value > 0) {
return 1;
}
else {
return -1;
}
}
});
return newlist.getAttribute(attribute.fulfill(1));
}
catch (Exception ex) {
dB.echoError(ex);
}
return newlist.getAttribute(attribute.fulfill(1));
}
});

// <--[tag]
// @attribute <li@list.sort[<procedure>]>
// @returns Element
Expand Down

0 comments on commit cb9b7a0

Please sign in to comment.