Doubt regarding sorting functions

P

praba kar

Dear All,

I am new to Python. I am in need of
some sorting functions (eg) numerical sorting
functions and alphapetical sorting functions.
I have searched through net But I cannot
find any regarding this so If anyone know
regarding this. Kindly mail me as early as possible

with regards
PRabahr

________________________________________________________________________
Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony
 
A

ajikoe

Basically you can sort after you put your data in your list:
L = [1.2,1.23,4.5,2]
L.sort()
print L

L = ["a","c","b","A","C","B"]
L.sort()
print L

Result:
[1.2, 1.23, 2, 4.5]
['A', 'B', 'C', 'a', 'b', 'c']

#------------------------------------------------------------
remember in character the "A" has lower ascii value then "a"

To model this as a function you just need this code for two of them:

def funcSort(myList):
result = myList[:]
result.sort()
return result

Use of this function:
L = [1.2,1.23,4.5,2]
res1 = funcSort(L)
print res1

L = ["a","c","b","A","C","B"]
res1 = funcSort(L)
print res1


Best Regards,
Pujo
 
B

Bengt Richter

Dear All,

I am new to Python. I am in need of
some sorting functions (eg) numerical sorting
functions and alphapetical sorting functions.
I have searched through net But I cannot
find any regarding this so If anyone know
regarding this. Kindly mail me as early as possible

with regards
PRabahr

Frankly, your claim to has "searched through the net" is not believable,
unless you are not using a search engine such as google, in which case
go to google.com (assuming you know how to do that) and type in something
like

sorting site:python.org

and press enter. The "site:python.org" will limit the search to
what is available on the python.org site, which has a high concentration
of Python-relevant information.

Perhaps this will help someone avoid a seemingly helpless or lazy post like yours.

BTW, a request to "mail me as early as possible" is more likely
to get favorable consideration if you explain why you can't
monitor the newsgroup postings for a reply.

Regards,
Bengt Richter
 
S

Steven Bethard

def funcSort(myList):
result = myList[:]
result.sort()
return result

In Python 2.4, funcSort is called sorted:

py> sorted([1.2,1.23,4.5,2])
[1.2, 1.23, 2, 4.5]
py> sorted(["a","c","b","A","C","B"])
['A', 'B', 'C', 'a', 'b', 'c']

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,799
Latest member
Mercury_Dev

Latest Threads

Top