-4

I have an array called names with a list of 4 names.

e.g

Names[1]='John'

I want to get the program to sort my array so that FOR each item in the array, except the last one, IF the item is bigger than the next one, swap the two items.

4
  • 2
    Are you asking "how do I sort a list of strings"? Commented Mar 29, 2012 at 18:04
  • Essentially. With the use of 4 identifiers. Commented Mar 29, 2012 at 18:04
  • I'm ... not sure what you mean by '4 identifiers'. Is what you want not just names.sort()? Commented Mar 29, 2012 at 18:06
  • You need to be more specific. What is example input and desired output. You are wasting our time and your own time if you are so unclear. Commented Mar 29, 2012 at 19:04

1 Answer 1

1

Like this??

names = [ 'Zac' , 'John',  'Andrew' , 'James' ]
for name in sorted(names[:-1]) + [names[-1]]:
    print name
Andrew
John
Zac
James
Sign up to request clarification or add additional context in comments.

6 Comments

This doesn't appear to work? Are you sure "[:-1]" is intentional?
I get the error: line 10, in <module> for Name in sorted(Names[:-1]) + Names[-1]: TypeError: unorderable types: str() < int()
The [:-1] is so that the last item in the list is excluded from the sort operation. We then append the last item onto the sorted list with + names[-1]
Python 3, and you've used different bracket types, around both times you've used Names, does that matter?
In python 3, the use of print was changed so that it's always called as a function. For the above code to work in python3, the print line needs to be changed to have parentheses around name. i.e. print(name)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.