Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a list of numerical strings in ascending order

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'] 
like image 865
linuxoid Avatar asked Mar 18 '12 14:03

linuxoid


People also ask

How do you sort a list of numerically strings in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically. Note: You cannot sort a list that contains BOTH string values AND numeric values.

How do you sort strings in ascending order?

Using the toCharArray() method Get the required string. Convert the given string to a character array using the toCharArray() method. Sort the obtained array using the sort() method of the Arrays class. Convert the sorted array to String by passing it to the constructor of the String array.

How do you sort a list containing strings?

In Python, there are two ways, sort() and sorted() , to sort lists ( list ) in ascending or descending order. If you want to sort strings ( str ) or tuples ( tuple ), use sorted() .


1 Answers

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.

like image 84
Óscar López Avatar answered Sep 20 '22 10:09

Óscar López