0

Python: dictionary sorting by key problem

I don't really understand what's wrong with my code and I can't think of anything because the output is messed up in a pretty strange way. I'm not an expert, started python & programming like 3 weeks ago code: https://pastebin.com/isq80i0K text: https://pastebin.com/4RdauWBC file(output): https://pastebin.com/qnuNnyLj This code should read the third number from text in each line, which is an identifier for a person and then it stores it in a dictionary to count how often a person shows up(value). Then I tried to sort this dictionary by value with a break after each line, but the sorting is kinda messed up, as you can see. Number 1 is good, then suddenly 10 comes and goes until 18, then randomly number 2, and it goes on. I don't understand what I messed up or why it is like that. For anyone wondering, some words in my code (like "athaladas", "ajto", "be/ki") are Hungarian.

15th Apr 2019, 4:26 PM
v v
v v - avatar
2 Réponses
+ 3
Everything is read as a string, so it is sorted lexicographically (like words in a dictionary). That's why '1' < '100' < '15' < '2' etc. In order to sort by numbers, convert the strings to numbers by using int()
15th Apr 2019, 5:51 PM
Anna
Anna - avatar
+ 1
Anna oh yes I see, I actually thought about that but I dont know why I was sure that these numbers were stored as integers. Pretty basic mistake :/. I'm very grateful, thanks a lot.
15th Apr 2019, 5:56 PM
v v
v v - avatar