It's possible but I wouldn't suggest you to cycle the entire collection to get the top 10 elements, that gonna be too expensive. Would be better if you actually save the top X elements during the elaboration.
Assume that you will have a method that gonna save the balance into your mapping
mapping(address => uint) balanceOf;
function pay() public payable {
require(msg.value > 0);
balanceOf[msg.sender] = msg.value;
}
you might change it adding the algorithm to actually calculate the top X elements just after you added the new one into your mapping
So your method gonna be something like
function pay2() public payable {
require(msg.value > 0);
balanceOf[msg.sender] = msg.value;
elaborateTopX(msg.sender, msg.value);
}
function elaborateTopX(address addr, uint currentValue) private {
uint i = 0;
/** get the index of the current max element **/
for(i; i < topBalances.length; i++) {
if(topBalances[i].balance < currentValue) {
break;
}
}
/** shift the array of position (getting rid of the last element) **/
for(uint j = topBalances.length - 1; j > i; j--) {
topBalances[j].balance = topBalances[j - 1].balance;
topBalances[j].addr = topBalances[j - 1].addr;
}
/** update the new max element **/
topBalances[i].balance = currentValue;
topBalances[i].addr = addr;
}
where topBalances
is an array defined in this way:
struct TopBalance {
uint balance;
address addr;
}
TopBalance[10] public topBalances;