list - Storing Doubles with a string - Java -
so seemed have run slight problem. attempting store doubles string (acting name them), able sort them in descending order.
i store them in someway, hashmap, arraylist, list etc... i'm not sure on best.
kind of lets assume these stored in way hashmap, list etc...
bob: 6.0 mary: 5.4 bill: 6.3 ann: 5.0 jim: 6.0
then output them like:
bill: 6.3 bob: 6.0 //notice, must able support duplicates. jim: 6.0 mary: 5.4 ann: 5.0
i hope makes sense others, if not let me know , can try clear up.
p.s. looked other threads not find 1 suits needs.
edit: seemed of found way works somehow... if interested in seeing, have pastebin link here: http://pastebin.com/qwjbd5mz
now, code built off bukkit api, may not useful others as could.
the easiest way create wrapper class:
class person { string name; double score; //constructor, getters etc. }
then put persons in list:
list<person> list = arrays.aslist(new person("bob", 6), new person("mary", 5.4), new person("bill", 6.3), new person("ann", 5.0), new person("jim", 6.0));
and sort them custom comparator compare scores:
collections.sort(list, comparator);
the comparator like:
collections.sort(list, new comparator<person>() { @override public int compare(person o1, person o2) { //sort score if (o1.score != o2.score) return double.compare(o1.score, o2.score); //if same score, sort name return o1.name.compareto(o2.name); } });
Comments
Post a Comment