51

I have created a sqlite database which has a table which stores temperature values. The temperature values are written to the database in ascending order for the first time. Then I read the temperature values from the database into a list and then add that list to a combo box to select temperatures - works fine.

The resulting list is, say:

templist = ['25', '50', '100', '150', '200', '250', '300'].

Then I add a new temperature value, say, '33' to the database.

It gets appended to the end of the table. If I read the temperatures now, the list will become:

['25', '50', '100', '150', '200', '250', '300', '33']. 

If I do templist.sort() or sorted(templist), the end result is

['150', '200', '25', '250', '300', '33', '50']

Is there any simple way to sort the list in ascending order so that I get:

['25', '33', '50', '100', '150', '200', '250', '300']
1
  • 8
    I'd say you have problems with the type of the things in your list. If they're sorting like that, they're probably strings instead of integers. Without seeing the code that gets them out of sqlite, and your schema, it's difficult to propose the right solution for you. Mar 18, 2012 at 14:10

2 Answers 2

74

The recommended approach in this case is to sort the data in the database, adding an ORDER BY at the end of the query that fetches the results, something like this:

SELECT temperature FROM temperatures ORDER BY temperature ASC;  -- ascending order
SELECT temperature FROM temperatures ORDER BY temperature DESC; -- descending order

If for some reason that is not an option, you can change the sorting order like this in Python:

templist = [25, 50, 100, 150, 200, 250, 300, 33]
sorted(templist, key=int)               # ascending order
> [25, 33, 50, 100, 150, 200, 250, 300]
sorted(templist, key=int, reverse=True) # descending order
> [300, 250, 200, 150, 100, 50, 33, 25]

As has been pointed in the comments, the int key (or float if values with decimals are being stored) is required for correctly sorting the data if the data received is of type string, but it'd be very strange to store temperature values as strings, if that is the case, go back and fix the problem at the root, and make sure that the temperatures being stored are numbers.

2
  • using order by will have the same problem if the data are strings. the underlying issue is that the data are not integers. Mar 18, 2012 at 14:21
  • 1
    sorted(templist, key=float) is exactly what I need. Thank you. I couldn't find that 'key=float' trick anywhere. I incorrectly showed (I'll change it above) that list that it's a list of strings, not a list of numbers. I had some problems dealing with floats in the database, mostly when searching for a particular value (if a float is not entirely equal to the number I search for, I didn't get any results), that's why I changed everything to strings. It seems easier to make various searches on strings rather than float numbers.
    – linuxoid
    Mar 18, 2012 at 23:37
29

in python sorted works like you want with integers:

>>> sorted([10,3,2])
[2, 3, 10]

it looks like you have a problem because you are using strings:

>>> sorted(['10','3','2'])
['10', '2', '3']

(because string ordering starts with the first character, and "1" comes before "2", no matter what characters follow) which can be fixed with key=int

>>> sorted(['10','3','2'], key=int)
['2', '3', '10']

which converts the values to integers during the sort (it is called as a function - int('10') returns the integer 10)

and as suggested in the comments, you can also sort the list itself, rather than generating a new one:

>>> l = ['10','3','2']
>>> l.sort(key=int)
>>> l
['2', '3', '10']

but i would look into why you have strings at all. you should be able to save and retrieve integers. it looks like you are saving a string when you should be saving an int? (sqlite is unusual amongst databases, in that it kind-of stores data in the same type as it is given, even if the table column type is different).

and once you start saving integers, you can also get the list back sorted from sqlite by adding order by ... to the sql command:

select temperature from temperatures order by temperature;
4
  • 2
    From the 3k sorted() doc: Return a new sorted list from the items in iterable.
    – Rik Poggi
    Mar 18, 2012 at 14:10
  • 1
    and on reflection that makes sense, because you need to look at an entire sequence to sort it, so you can't have a sorting generator that avoids an internal list. Mar 18, 2012 at 14:18
  • Why not sort the list in place? Mar 18, 2012 at 14:46
  • you could; i'll add that too! (the reaosn i didn't is that more and more i'm just used to generators in python and lazy sequences in clojure and guava's iterables in java - it's a really useful way of doing work, but you forget that can mutate data...) Mar 18, 2012 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.