0

i have json file:

ll = {"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"James", "lastName":"Bond"},
    {"firstName":"Celestial", "lastName":"Systems"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

i want to sort by key(lastname or firstname)

i know the sorting method that we use with number as value but i am not able to sort this by value as string.

i tried below:

#data = json.load(file)
new = sorted(data, key = lambda k: k['employees'].get('lastName',0))

and getting error:

TypeError: string indices must be integers

2 Answers 2

1

You need to give list of objects to sorted and key should be method to get value for single object.

In [1]: ll = {"employees": [{"name": "abc"}, {"name": "bcd"}]}

In [2]: sorted(ll['employees'], key=lambda x: x['name'])
Out[2]: [{'name': 'abc'}, {'name': 'bcd'}]

As you can see I get list of employees to sorted. Then sorted run lambda on every element of that list (so it calls x['name'] on {"name": "abc"} and gets name. Then sorted sort elements in list using key (it doesn't matter if it's string or number - string are also numbers somehow - ascii).

When you have sorted list you can assign it to ll['employees'] or any other place.

Anyway, guessing your code (cause you didn't include all of it) I assume that you're calling sorted on dict, not list. Also you're using wrong key (k is not full object, but single element in list passed to sorted).

Sign up to request clarification or add additional context in comments.

Comments

1

You can sort the ll["employees"] list in-place using the list.sort method. And while you can roll your own key function using lambda it's cleaner and more efficient to use operator.itemgetter.

In this code, my key function creates a tuple of the lastName followed by the firstName, so the list is primarily sorted by lastName, but people with the same lastName are sorted by firstName.

from operator import itemgetter
from pprint import pprint

ll = {"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Patti", "lastName":"Smith"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"James", "lastName":"Bond"},
    {"firstName":"Celestial", "lastName":"Systems"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

ll["employees"].sort(key=itemgetter("lastName", "firstName"))
pprint(ll)

output

{'employees': [{'firstName': 'James', 'lastName': 'Bond'},
               {'firstName': 'John', 'lastName': 'Doe'},
               {'firstName': 'Peter', 'lastName': 'Jones'},
               {'firstName': 'Anna', 'lastName': 'Smith'},
               {'firstName': 'Patti', 'lastName': 'Smith'},
               {'firstName': 'Celestial', 'lastName': 'Systems'}]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.