I used JavaScript for a use case like this recently.
function getRank(clicksJoined, clicksCount) {
// Split the joined clicks string into an array of numbers
const clicksArray = clicksJoined.split(', ').map(Number);
// Sort the clicks array in descending order
clicksArray.sort((a, b) => b - a);
// Initialize rank and previous click count
let rank = 1;
let previousCount = clicksArray[0];
// Iterate through the sorted clicks array to determine the rank
for (let i = 1; i < clicksArray.length; i++) {
if (clicksArray[i] < previousCount) {
rank = i + 1;
previousCount = clicksArray[i];
}
if (clicksArray[i] === clicksCount) {
return rank;
}
}
// If the clicksCount is the highest, return 1
if (clicksCount === clicksArray[0]) {
return 1;
}
// If the clicksCount is not found in the array, return -1 (indicating not ranked)
return -1;
}
return getRank(p1, p2)
p1: Comma-delimited list of all “scores”
p2: Current row’s score
Gets something like this.