Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 6th, 2006, 11:32 AM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile Case Insensitive Sorting

I want to sort a list of words case insensitive. Is there a good way?

I have:
print "A list of Python builtin methods:"
builtinList = dir(__builtins__)
print builtinList
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 6th, 2006, 11:36 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
The sort method in lists allows you to specify a comparison function:
buildinList.sort(lambda a, b : cmp(a.lower(), b.lower()))
Or:
def icmp(a, b):
    return cmp(a.lower(), b.lower())
buildinList.sort(icmp)
Arevos is offline   Reply With Quote
Old Apr 6th, 2006, 12:31 PM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thanks Arevos,

I was also looking at the new "key=" specifier. Is there something you can enter there to do the trick?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 6th, 2006, 12:55 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Dietrich
I was also looking at the new "key=" specifier. Is there something you can enter there to do the trick?
Huh. So there is. Didn't know about that one before, but this seems to do the trick:
builtinList.sort(key = str.lower)
Arevos is offline   Reply With Quote
Old Apr 6th, 2006, 11:43 PM   #5
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Strange, but it does work! You are a clever person after all!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC