java - Ranking objects in a list -
i have list
of objects, each object has number of votes, so:
object votes o1 5 o2 4 o3 3 o4 3
i want rank each 1 (not sort) based on number of votes , create map
using result. result be:
object votes rank o1 5 1 o2 4 2 o3 3 3 o4 3 3
so can see o3 , o4 have same rank because have same number of votes. there fast implementation this?
first collections.sort list, iterate through list. if number of votes has changed since lastly object, increment rank, if the votes count same, don't add together object/rank map.
// not tested, should give right idea. collections.sort(mylist); // may need utilize comparator here if objects don't implement comparable int rank = 0; int lastvotes = -1; (myobject o : list) { if (o.getvotes() != lastvotes) { rank += 1; } mymap.put(o, rank); lastvotes = o.getvotes(); }
java list collections ranking
No comments:
Post a Comment