How to rank members in a contest?

I have Ranking as a Data Type where I keep the points earned for each member. Ranking has Name, Points and Rank as the data fields. For example, if I have the follow data in Ranking with Name and Points.

Member1 10
Member2 10
Member3 7
Member4 5
Member5 5
Member6 3
Member7 1

I like the Rank to be:

1
1
2
3
3
4
5

How do I do that in Bubble? How can I write loop that will iterate through the Ranking list and assign proper Rank to each member?

Please help. Thanks!

I’m trying to use the server script to do it.

Here is the server script setup:

Here is the script:

var i;
var rank = 1;
properties.thinglist1[0].Position = rank;
for (i=1; i<properties.thinglist1.length; i++) {
if (properties.thinglist1[i].Points == properties.thinglist1[i-1].Points) {
properties.thinglist1[i].Position = rank;
} else if (properties.thinglist1[i].Points > properties.thinglist1[i-1].Points) {
rank += 1;
properties.thinglist1[i].Position = rank;
}
}

This doesn’t work at all. Position and Points are not addressable in the script? Can you help make this work?

1 Like

How are you displaying the rank? What’s the purpose of the rank?

I’m going to use the repeating group to display them. So it will something like:

Rank Name Points
1 Member 1 10
1 Member 2 10
2 Member 3 7
3 Member 4 5
3 Member 5 5
4 Member 6 3
5 Member 7 1

The purpose is so that Members can see where they are ranked on the leader board page.

You can just use the :sorted command in the repeating group and then choose the database field that you wish to sort by (rank) and choose descending is “yes”

So if I get you correctly, essentially your Users have points. If you sort a list of Users by points (descending) and secondarily by some other factor (User’s name? Last time they played? I dunno… pick anything), there’s your “Rank” amongst those Users.

Rank is a derived value based on points. It may not really make sense to write Rank as part of a Ranking as a User’s Rank is actually regularly changing.

The style of ranking you’re doing here is sometimes called “dense” ranking. It’s almost like simply using the index of the RG you’d display a sorted list by points in, but adjusted for how many duplicated ranks (“ties”) there are in the list.

There must be a fairly simple algorithmic way to display this as you did in your mockup above:

It’s something like:

The Rank # is the current cell's index minus (the difference between the number of rows above [which is current cell's index -1] and the number of unique scores in the rows above [which is the RG's list of things's Points:unique items:items up to current cell's index:count])

It just needs a bit of playing around with but the above should put you on the right track…?

3 Likes

In fact, I was very close to being right:

Look at the rows from top to bottom:

Row 1 is the leader (1st place): Index 1 should always be ranked as 1 of course.
Row 2 is tied for leader (1st place): Index 2 - (2 - 1 unique score to this point) = 1
Row 3 is 2nd place: Index 3 - (3 - 2 unique scores to this point) = 2
Row 4 is 3rd place: Index 4 - (4 - 3 unique scores to this point) = 3
Row 5 is tied for 3rd place: Index 5 - (5 - 3 unique scores to this point) = 3
Row 6 is 4th place: Index 6 - (6 - 4 unique scores to this point) = 4

Etc…

So the text box in your RG under rank should print out:

Current Cell’s Index - RG’s List of Things’s:Items until Current Cell’s Index:unique items:count *-1 + Current Cell’s index… (if I’m not mistaken… the wonky way of writing this expression is due to Bubble’s left to right order of evaluation…

Anyway, it’s something like that.

4 Likes

Thanks Keith! Wow… so awesome! I made a small modification and it works. Here is the modification on the calculation:

rank

4 Likes

Oh… HA HA HA! There’s clearly some algebraic simplification I didn’t do there.

But I’m SO GLAD you followed along with me and figured out how to do this! This is the type of thought process I go through all of the time to create new features in Bubble.

Now that you know how to do these crazy compound type computations, you’ll find yourself using them all over. It’s just a different sort of math than we are used to.

(Thinking list-wise [as opposed to thinking “loop-wise” – iterating over an array of things multiple times “in your head”] is a form of parallel computing. It’s very powerful, but a little tweaky sometimes as we sort of default to thinking about things in the loop-wise way. Visualizations – like your very smart writing out of “here is how I want it to look” – are key in figuring this kind of stuff out.)

3 Likes

You guys just saved me a tonne of time. Thanks!

Would anyone know how to do this on a Backend Workflow?

I’m trying to schedule an API workflow on a list, but it’s not giving an option for current cells index's

So I tried this instead:
Screen Shot 2021-07-09 at 8.30.22 PM

Which does not work.

P.S - thanks Thanks @Keith and @greenbamboosolutions for the awesome thread.

I know it’s been a long time.
Were you able to do it?