There are number of possible ways you might achieve this.
Assuming we have a List
as follows...
List<Integer> marks = new ArrayList<>(5);
marks.add(430);
marks.add(400);
marks.add(372);
marks.add(500);
You could simply do...
Collections.sort(marks);
int row = 0;
for (Integer mark : marks) {
System.out.println((++row) + ": " + mark);
}
This will output...
1: 372
2: 400
3: 430
4: 500
But wait, that's not what we want!
But with the sorted list, we can do...
Collections.reverse(marks);
row = 0;
for (Integer mark : marks) {
System.out.println((++row) + ": " + mark);
}
Which outputs...
1: 500
2: 430
3: 400
4: 372
Much better :D
Now, if that seems like to much work, you can also use a Comparator
to change the way that the items are sorted...
Collections.sort(marks, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return (o2 - o1);
}
});
Which will produce a list in the following order...
1: 500
2: 430
3: 400
4: 372
Updated
Based on the the answer from Iterating through a list in reverse order in java, you could also do a reverse iterations of the list, if all you want to do is display the result (thanks to Boris the Spider)
Collections.sort(marks);
int row = 0;
ListIterator li = a.listIterator(a.size());
while(li.hasPrevious()) {
System.out.println(li.previous());
}
Updated with line weights
If you're interested in knowing the "weight" on the mark, you need to modify the line marker so that it only increments when the last mark and the next mark are no longer equal...
List<Integer> marks = new ArrayList<>(Arrays.asList(new Integer[]{430, 400, 372, 400, 500}));
Collections.sort(marks, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return (o2 - o1);
}
});
int row = 0;
int last = -1;
for (Integer mark : marks) {
if (mark != last) {
row++;
}
System.out.println(row + ": " + mark);
last = mark;
}
Which outputs...
1: 500
2: 430
3: 400
3: 400
4: 372
Now, if you wanted to the output to be more like...
1: 500
2: 430
3: 400
3: 400
5: 372 // Not the skipped line...
You would need to know how many times a individual mark was matched and increment the "row" accordingly...
int row = 0;
int last = -1;
int matches = 1;
for (Integer mark : marks) {
if (mark != last) {
row += matches;
matches = 1;
} else {
matches++;
}
System.out.println(row + ": " + mark);
last = mark;
}