Python So, is there any explanation for this?Why can't python do a numerical sort?

AI Thread Summary
Python can perform numerical sorting, but it requires the data to be in the correct format. Users expressed frustration over sorting strings that contain numbers, as Python defaults to alphabetical sorting. To achieve numerical sorting, it's necessary to convert strings to integers. The recommended approach involves using list comprehension or the `sort()` method with a custom key function. For example, using `int(a.split()[0])` allows for sorting based on the numerical value extracted from strings. For lists of integers, the built-in `sort()` method works seamlessly across platforms, including Windows. Users highlighted that Python's sorting capabilities are straightforward when the data is properly formatted as integers. Additionally, resources such as the official Python documentation and "Dive Into Python" are suggested for further learning. Overall, Python's sorting functionality is robust, but understanding data types and proper formatting is crucial for effective use.
nameta9
Messages
184
Reaction score
0
No numerical sort in python ?

I was really surprised to see that python can't do such an elementary operation!

I am trying to do a numerical sort in python on an array. I am using python for windows (DOS). In perl you can simply write sort{$a<=>$b}@a and this will sort it according to the value of the numerical string, (not the alphabetical value). I am looking for something similar in python but could only find a reference to sourceforge numpy where I have to download a large piece of source code in C and recompile it locally. Is there a simpler solution ? thanks...
 
Last edited:
Technology news on Phys.org
x.sort(lambda a,b: cmp(int(a), int(b)))

where x is the list you want to sort. Of course, you could just say x.sort() if you just stored your list of numbers as an actual list of numbers and not a list of strings.
 
Thanks master. Getting close but it doesn't seem to work. My list is of the format x[1]="1123 wwww", x[2]="345 asdd", etc. What should I put in int(a) and int(b) ? when this is sorted "1123 wwwww" is before "345 asdd" because it is doing an alphabetical sort and not a numerical sort. Thanks for any suggestions.
 
Try int(a.split()[0]) and int(b.split()[0]).
 
It doesn't even do this! I also tried with a.append(22) etc. I don't think python sorts numerically in any case! Maybe it is because python on windows is too immature ?

a=[]
a.append(int('22'))
a.append(int('111'))
a.append(int('9'))
a.append(int('12'))

a.sort()

for mat in a:print mat


Second EDIT::::

OK it has to be done like this:

def descendingCmp(a,b):
return cmp(a,b)

a=[]
a.append(22)
a.append(111)
a.append(9)
a.append(12)

a.sort(descendingCmp)

for mat in a:print mat


It is simpler in perl, maybe they should simply create a built in function for sort numeric...
 
Last edited:
So, you are decrying python because you did not take the time to even quickly read through the python docs? You don't need to declare variable types in python. 4 is a number and '4' is a string.

Here, try this:
Code:
a=[123,234,542,821,111,54,767,23453,90,9,0,1,321]
a
a.sort()
a
it will work on windows, mac, linux, unix...

If you really want to get crazy do this:
Code:
a=[(123,'jack'),(1,'work'),(234,'a'),(542,'boy'),(111,'makes'),(90,'play'),(9,'and'),(0,'all'),(321,'dull'),(54,'no')]
a
a.sort()
a
it's easy.

Here is a good source for python knowledge:
http://www.python.org/doc/2.4.1/tut/tut.html

as is this:
http://diveintopython.org/

You should thumb through the docs and brush up a little. Python is day and night easier than perl.
 
Last edited by a moderator:
nameta9 said:
It doesn't even do this! I also tried with a.append(22) etc. I don't think python sorts numerically in any case! Maybe it is because python on windows is too immature ?

a=[]
a.append(int('22'))
a.append(int('111'))
a.append(int('9'))
a.append(int('12'))

a.sort()

for mat in a:print mat

This code works fine for me. When I run it I get:

9
12
22
111
 

Similar threads

Replies
10
Views
3K
Replies
5
Views
1K
Replies
2
Views
22K
Replies
4
Views
7K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
6
Views
7K
Back
Top