I am trying to create a leaderboard depending upon who has received maximum votes. I have implemented the following code:
function dashboard(bytes32[] candidateListNew) public returns(bytes32[])
{ for(uint i = 0; i < candidateListNew.length; i++){
for(uint j = i+1; j < candidateListNew.length; j++){
if(votesReceived[candidateListNew[i]] > votesReceived[candidateListNew[j]]){
uint8 temp = votesReceived[candidateListNew[j]];
votesReceived[candidateListNew[j]] = votesReceived[candidateListNew[i]];
votesReceived[candidateListNew[i]] = temp;
}
}
}
return votesReceived[candidateListNew]; //gives me conversion type error
}
struct voter {
bytes32 votedFor;
bool hasVoted;
}
mapping (bytes32 => uint8) public votesReceived;
// Mapping for persons who have already voted
mapping (bytes32 => voter) public voters;
bytes32[] public candidateList;
bytes32[] public candidateListNew;
I am able to sort the values but I am having difficulty sorting the keys(candidate names) based on the values.
Any help would be appreciated. Thank you!!
return votesReceived[candidateListNew];the type error is obvious. Your mapping goes from bytes32 to uint8 and you are using candidateListNew as index, which is an array of bytest32 and not a bytes32 as expected. – Briomkez Jul 31 '18 at 15:46