473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how do I sort a list of names?

I need to sort a list of first and last names from shortest to longest.
for example:
[John Doe, Jane Smith, Jane Doe]

I have to write a function to find the longest name but noe sure how to do it!
Nov 30 '10 #1
2 2617
bvdet
2,851 Expert Mod 2GB
In Python 2.3:
Expand|Select|Wrap|Line Numbers
  1. >>> names = ["John Doe", "Jane Smith", "Jane Doe", "Jo Nu"]
  2. >>> names.sort(lambda x, y: cmp(len(x), len(y)))
  3. >>> names
  4. ['Jo Nu', 'John Doe', 'Jane Doe', 'Jane Smith']
  5. >>> 
Dec 1 '10 #2
dwblas
626 Expert 512MB
I have to write a function to find the longest name but no(t) sure how to do it!
Do you want to sort or to find the longest name. Sorting the entire list just to find one item is very inefficient and if I was your instructor, would be rejected as too inefficient to be a viable solution.
Expand|Select|Wrap|Line Numbers
  1. ##   does not allow for multiple names that are longest
  2. names = ['Jon Doe', 'Jane Smith', 'Jane Doe']
  3. longest_name = names[0]
  4. longest = len(names[0])
  5.  
  6. for name in names:
  7.     if len(name) > longest:
  8.         longest = len(name)
  9.         longest_name = name
  10. print longest_name, longest 
Dec 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: JC | last post by:
Hello, I'm running into a huge wall with linked lists. If anyone outhere can help me, I'll appreciate it very much. Here is my delima! I need to create a simple list of numbers. which I...
19
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray =...
3
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
1
by: VMI | last post by:
I have a Windows datagrid whose column values are (1,10,100,1000,101,10-1,103,11,1710,2). When the column's sorted (the column's of type String), it'll sort this way , which is wrong. But in...
7
by: Lad | last post by:
I have a list of emails and I would like to sorted that list by domains E.g. If the list is Emails= after sorting I would like to have Emails= What is the best/easiest way?
6
by: Tem | last post by:
Thanks for all your responses. I see why c# is such a powerful language and starting to like it more and more. I also need help sorting this nested list. It is a little tricky. not sure where to...
5
by: jr | last post by:
I created a simple class, have used it to populate a list (of myclass) - The class only contains a customer id and a date. I would like to sort this list based on most recent date to the oldest....
3
by: Tem | last post by:
What's a good way to sort a List<stringby the length of the strings from longest to shortest. in c# 3 "aaaaaa" "aaaaaaaaaa" "aaa" becomes "aaaaaaaaaa" "aaaaaa"
2
by: rkartheek | last post by:
I want to generate a list of table names into a file in the order of FK dependencies. The purpose is to truncate all tables in a given database/schema by using the generated file in a "for loop". I...
3
by: fastestindian | last post by:
Hi, I am working on the project where i show a list of Log Records. my list is as follows. Original list Id Time Event Details 1 1 Error1 xyz 2 1 ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.