I've a website in reactjs and with a backend written in python. Now, I've a list of photos and I want sort this by a key called "score".
In python I do this (pls, tell me if it's safe to do queries in this way, I'm using MySQLdb with Flask):
sql = "SELECT id, image_url, score, width, height FROM images ORDER BY score DESC LIMIT 10 OFFSET %s"
conn.cursor.execute(sql, (offset,))
images = conn.cursor.fetchall()
dictImages = {}
for image in images:
dictImages[image[0]] = {"url": image[1], "score": image[2], "width": image[3], "height": image[4]}
and my dictImages is:
98: {'url': '62eb97f4496e9bd19755426bde78528f760f1e231545594871.jpg', 'score': 1459, 'width': 720, 'height': 1080},
100: {'url': '5a3a28f6f174d5995e6b6d6882e7c055c324f0081545595509.jpg', 'score': 1400, 'width': 1080, 'height': 1080},
97: {'url': '2516629bb990e9e8e6a4ccdaeae4048aa83119ee1545550157.JPG', 'score': 1390, 'width': 720, 'height': 899},
96: {'url': '6d3236ec3c88039ca534b81acad564e847ecb0621545549723.jpg', 'score': 1381, 'width': 640, 'height': 640},
99: {'url': '7a09101b84cd5618bfcdfa8423c16232788bf9691545595051.png', 'score': 1370, 'width': 1080, 'height': 940}
As you can see it is ordered by score, but the js object isn't, it is:
"96": {
"height": 640,
"score": 1381,
"url": "6d3236ec3c88039ca534b81acad564e847ecb0621545549723.jpg",
"width": 640
},
"97": {
"height": 899,
"score": 1390,
"url": "2516629bb990e9e8e6a4ccdaeae4048aa83119ee1545550157.JPG",
"width": 720
},
"98": {
"height": 1080,
"score": 1459,
"url": "62eb97f4496e9bd19755426bde78528f760f1e231545594871.jpg",
"width": 720
},
"99": {
"height": 940,
"score": 1370,
"url": "7a09101b84cd5618bfcdfa8423c16232788bf9691545595051.png",
"width": 1080
},
"100": {
"height": 1080,
"score": 1400,
"url": "5a3a28f6f174d5995e6b6d6882e7c055c324f0081545595509.jpg",
"width": 1080
}
It's ordered by key. How can I avoid this problem? And how can I order it efficiently? (Now I've only few keys, but suppose to have much more..)
Thank you for any advise!