MatejMecka
MatejMecka

Reputation: 1486

Django Ordering by Descending Order is glitched

So I have made a leaderboard in Django. It does most stuff Great except something it's drunk.

enter image description here

That's not normal! 70 Is higher than 540 and 200! It should be in 3rd Place not 1st! Why is that happening and how can I fix it.

I'm ordering the leaderboard using the Total or totalpoints in Descending order using this: participants = Participant.objects.order_by('-totalpoints')

This didn't happen when 70 was missing. Now when it's there it glitches. Also the Total points is added from all the challenges and if there was a problem eithher an ASCII or something else it would have crashed.

Any Help is going to be appreciated thank you!

EDIT: Replaced the last field as an IntegerField and now it's in Ascending Order...

EDIT EDIT: When the challenge 1,2,3,4 are combined they are in CharFields later converted to integers. So it was an Integer put into a field. Except when the totalpoints is now replaced with an IntegerField the leaderboard is ordering the emptry ones first.

Upvotes: 0

Views: 169

Answers (2)

meshy
meshy

Reputation: 9076

You must be storing your totalpoints as a string instead of an integer. This will mean you're seeing the numbers ordered "alphabetically" (in reverse, because of the -).

If you store the totalpoints field in an IntegerField, you should have a better result.

Upvotes: 1

Ned Batchelder
Ned Batchelder

Reputation: 375494

You haven't shown any code, so it's hard to say for sure, but this looks like a case of sorting numeric strings. Your totalpoints data isn't numbers, it's strings.

If you sort a list of strings which happen to look like numbers, they don't sort like numbers. The first digit is the most significant, and will determine the sort order:

>>> sorted(['200', '70', '540'], reverse=True)
['70', '540', '200']

If you sort them as numbers, they work correctly:

>>> sorted([200, 70, 540], reverse=True)
[70, 200, 540]

Upvotes: 1

Related Questions