Skip to content

Instantly share code, notes, and snippets.

@DickyT
Created October 26, 2016 04:04
Show Gist options
  • Save DickyT/44816fd11bc27e36b2593b16a418286b to your computer and use it in GitHub Desktop.
Save DickyT/44816fd11bc27e36b2593b16a418286b to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* Created by KanzakiMirai on 10/26/16.
*/
public class Test {
public static void main(String[] args) {
Demo tmp = new Demo();
tmp.main();
}
}
class Demo {
ArrayList<Hero> arrayList = new ArrayList<>();
void main() {
this.arrayList.add(new Hero("Reaper", 250));
this.arrayList.add(new Hero("Mei", 250));
this.arrayList.add(new Hero("Mercy", 200));
this.arrayList.add(new Hero("D.VA", 600));
this.arrayList.add(new Hero("Roadhog", 600));
System.out.println("arrayList = " + arrayList);
Comparator<Hero> comparator = (o1, o2) -> o2.health - o1.health;
Collections.sort(this.arrayList, comparator);
System.out.println("this.arrayList = " + this.arrayList);
}
}
class Hero {
public String name;
public Integer health;
Hero(String name, Integer health) {
this.name = name;
this.health = health;
}
@Override
public String toString() {
return String.format("name: %s, health: %d", this.name, this.health);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment