Groovy web console

subscribe to the feed Subscribe
to this
site
Simple sort by number attr (via #groovywebconsole)
tweet this snippet Tweet
this
script

Simple sort by number attr

Published 6 months ago by clbcabral
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class P implements Comparable<P> {
    public int n;
    public P(int n){this.n=n}
    public String toString(){return ""+n}
    public int compareTo(P p) {p.n - this.n}
}

List a = Arrays.asList(new P(2), new P(1), new P(7), new P(-5), new P(0), new P(11));

Collections.sort(a);

print(a);