0
def salary_sort(thing):
    def importantparts(thing):
        for i in range(1, len(thing)):
            a=thing[i].split(':')
            output = (a[1],a[0],a[8])
            sortedlist = sorted(output, key = lambda item: item[2], reverse=True)
            print(sortedlist)
    return importantparts(thing)

salary_sort(employee_data)

This function is supposed to sort out a list of names by their salary. I managed to isolate the first last names and salaries but I can't seem to get it to sort by their salaries

'Thing' aka employee_data

employee_data = ["FName  LName   Tel      Address  City   State  Zip   Birthdate   Salary",
"Arthur:Putie:923-835-8745:23 Wimp Lane:Kensington:DL:38758:8/31/1969:126000",
"Barbara:Kertz:385-573-8326:832 Ponce  Drive:Gary:IN:83756:12/1/1946:268500",
"Betty:Boop:245-836-8357:635 Cutesy Lane:Hollywood:CA:91464:6/23/1923:14500",.... etc.]

Output

['Putie', 'Arthur', '126000']
['Kertz', 'Barbara', '268500']
['Betty', 'Boop', '14500']
['Hardy', 'Ephram', '56700']
['Fardbarkle', 'Fred', '780900']
['Igor', 'Chevsky', '23400']
['James', 'Ikeda', '45000']
['Cowan', 'Jennifer', '58900']
['Jesse', 'Neal', '500']
['Jon', 'DeLoach', '85100']
['Jose', 'Santiago', '95600']
['Karen', 'Evich', '58200']
['Lesley', 'Kirstin', '52600']
['Gortz', 'Lori', '35200']
['Corder', 'Norma', '245700']
  • 1
    Also, could you give an example of `thing` you used for testing ? – 3kt Oct 11 '16 at 06:39
  • Most likely (depending where you put it) you should use `a.sort()` instead of `sorted()`. You are not reusing the returned list, `sortedlist`. `sorted()` does not sort in place, it returns the new sorted list. – cdarke Oct 11 '16 at 06:42
  • The code looks fine, as well as the sorting. You sure you have `a` as a list of tuples and not just a tuple? would you mind providing content of `a`? – lycuid Oct 11 '16 at 06:42
  • Do you want the third column in *numeric* order? In which case the `key` should be `key = lambda item: int(item[2])`. The salaries are strings, so by default it will sort in textual order (where "9" is higher than "10"). – cdarke Oct 11 '16 at 07:02
  • I want the entire ouput to be sorted by the third column, sort of like; [joe, bill, 30000] [ann, lee, 200] [fox, dog, 25] – kaleidliner Oct 11 '16 at 07:08

3 Answers3

2

There are a number of issues with your code, but the key one is that you are sorting each row as you create it, rather than the list of lists.

Also:

  • importantparts() doesn't return anything (so salarysort() returns None).

  • You need to cast the Salary field to an int so that it sorts properly by value (they don't all have the same field-width, so an alphanumeric sort will be incorrect).

  • Finally, you don't need to use for i in range(1, len(thing)):, you can iterate directly over thing, taking a slice to remove the first element1.

1Note that this last is not wrong per se, but iterating directly over an iterable is considered more 'Pythonic'.

def salary_sort(thing):
    def importantparts(thing):
        unsortedlist = []
        for item in thing[1:]:
            a=item.split(':')
            unsortedlist.append([a[1],a[0],int(a[8])])
        print unsortedlist
        sortedlist = sorted(unsortedlist, key = lambda item: item[2], reverse=True)
        return (sortedlist)
    return importantparts(thing)

employee_data = ["FName  LName   Tel      Address  City   State  Zip   Birthdate   Salary",
                 "Arthur:Putie:923-835-8745:23 Wimp Lane:Kensington:DL:38758:8/31/1969:126000",
                 "Barbara:Kertz:385-573-8326:832 Ponce  Drive:Gary:IN:83756:12/1/1946:268500",
                 "Betty:Boop:245-836-8357:635 Cutesy Lane:Hollywood:CA:91464:6/23/1923:14500"]


print salary_sort(employee_data)

Output:

[['Kertz', 'Barbara', 268500], ['Putie', 'Arthur', 126000], ['Boop', 'Betty', 14500]]
Community
  • 1
  • 1
SiHa
  • 7,830
  • 13
  • 34
  • 43
1

You main problem is that you reset the output sequence with each new line instead of first accumulating the data and then sorting. Another problem is that your external function declared an inner one and called it, but the inner one did not return anything. Finally, if you sort strings without converting them to integers, you will get an alphanumeric sort: ('9', '81', '711', '6') which is probably not what you expect.

By the way, the outer-inner functions pattern is of no use here, and you can use a simple direct function.

def salary_sort(thing):
    output = []
    for i in range(1, len(thing)):
        a=thing[i].split(':')
        output.append([a[1],a[0],a[8]])
    sortedlist = sorted(output, key = lambda item: int(item[2]), reverse=True)
    return sortedlist

the result is as expected:

[['Kertz', 'Barbara', '268500'], ['Putie', 'Arthur', '126000'], ['Boop', 'Betty', '14500']]

If you prefer numbers for the salaries, you do the conversion one step higher:

def salary_sort(thing):
    output = []
    for i in range(1, len(thing)):
        a=thing[i].split(':')
        output.append([a[1],a[0],int(a[8])])
    sortedlist = sorted(output, key = lambda item: item[2], reverse=True)
    return sortedlist

and the result is again correct:

[['Kertz', 'Barbara', 268500], ['Putie', 'Arthur', 126000], ['Boop', 'Betty', 14500]]
SiHa
  • 7,830
  • 13
  • 34
  • 43
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

The problem is that you sort individual elements (meaning ['Putie', 'Arthur', '126000']), based on the salary value, and not the whole array.

Also, since you want to sort the salaries, you have to cast them to int, otherwise alphabetical sort is going to be used.

You can take a look at the following :

def salary_sort(thing):
    def importantparts(thing):

        data = []

        for i in range(1, len(thing)):
            a=thing[i].split(':')
            output = (a[1],a[0],int(a[8]))
            data.append(output)

        data.sort(key=lambda item: item[2], reverse=True)
        return data

    return importantparts(thing)

employee_data = ["FName  LName   Tel      Address  City   State  Zip   Birthdate   Salary", \
"Arthur:Putie:923-835-8745:23 Wimp Lane:Kensington:DL:38758:8/31/1969:126000", \
"Barbara:Kertz:385-573-8326:832 Ponce  Drive:Gary:IN:83756:12/1/1946:268500", \
"Betty:Boop:245-836-8357:635 Cutesy Lane:Hollywood:CA:91464:6/23/1923:14500"]

print(salary_sort(employee_data))

Which gives, as expected :

[('Kertz', 'Barbara', 268500), ('Putie', 'Arthur', 126000), ('Boop', 'Betty', 14500)]

What I did there is pushing all the relevant data for the employees into a new array (named data), and then sorted this array using the lambda function.

3kt
  • 2,543
  • 1
  • 17
  • 29