I have a class of Student. The class has String name
, int studentId
and int studentScore
. I want to compare the students in ArrayList<Student> students
according to the studentscore. This means if "student 1" and "student 2" has the same amount of studentscore then both of their ranks will be the same. I have a function where I set The rank. But I'm unsure how to compare everything in the arraylist.
When I search for comparing int values I get the results:
How to properly compare two Integers in Java?
I think the best solution will be to use comparator class function. But Im unsure how to do this because I'm unsure of the comparator classes capability. I was think of for looping getting a player by that loop then for looping again and comparing. But this doesnt seem like a good solution.
I want this result:
Say that the array of students contains (name,studentscore) is:
(kim, 12), (werder, 20), (ben, 32), (sara, 12)
I want the rank of the student to be (If ASC order):
kim: rank = 1
werder: 2
ben: 3
sara: 1