-2

I have a list 'd8' which has 3 items

>>> d8[0]
{u'lix_edu_dev_override': u'control', u'fmt__school_highlight': u'Indian Institute of Social Welfare & Business Administration, Kolkata', u'degree': u'Master of Business Administration (MBA)', u'i18n__detailed_school_page': u'More details for this school', u'lix_treasury_upload': u'B', u'fosList': [{u'link__fos_pivot': u'/search?search=&keywords=Marketing&sortCriteria=R&keepFacets=true&trk=prof-edu-field_of_study', u'fmt__fos_highlight': u'Marketing', u'name': u'Marketing', u'i18n__find_others': u'Find users with this keyword'}], u'enddate_iso': u'1993', u'fieldOfStudy': u'Marketing', u'link__peer_tracker_pivot_with_name': u'/college/?eduSchool=Indian+Institute+of+Social+Welfare+%26+Business+Administration%2C+Kolkata&trk=prof-edu-school-name', u'educationId': 134727074, u'fmt__degree_highlight': u'Master of Business Administration (MBA)', u'i18n__other_alumni': u'Find other members who attended Indian Institute of Social Welfare & Business Administration, Kolkata', u'startdate_iso': u'1991', u'schoolName': u'Indian Institute of Social Welfare & Business Administration, Kolkata', u'link__school_logo': u'/edu/school', u'lix_edu_profile_link': u'control', u'startdate_my': u'1991', u'enddate_my': u'1993', u'lix_edu_mvp_override': u'control'}

>>> d8[1]
{u'lix_edu_dev_override': u'control', u'link__peer_tracker_pivot_with_id': u'/college/?eduSchool=20479&trk=prof-edu-school-name', u'degree': u'B.Sc.', u'i18n__detailed_school_page': u'More details for this school', u'lix_treasury_upload': u'B', u'fosList': [{u'link__fos_pivot': u'/search?search=&keywords=physics&sortCriteria=R&keepFacets=true&trk=prof-edu-field_of_study', u'fmt__fos_highlight': u'physics', u'name': u'physics', u'i18n__find_others': u'Find users with this keyword'}], u'enddate_iso': u'1991', u'fieldOfStudy': u'physics', u'lix_edu_profile_link': u'control', u'schoolId': 20479, u'educationId': 15866086, u'fmt__degree_highlight': u'B.Sc.', u'i18n__other_alumni': u"Find other members who attended St. Xavier's College", u'fmt__school_highlight': u"St. Xavier's College", u'startdate_iso': u'1988', u'schoolName': u"St. Xavier's College", u'link__school_logo': u'/edu/school?id=20479', u'startdate_my': u'1988', u'enddate_my': u'1991', u'lix_edu_mvp_override': u'control'}

>>> d8[2]
{u'lix_edu_dev_override': u'control', u'fmt__school_highlight': u"St. Xavier's School Loyola Hall", u'degree': u'H.S.C.', u'i18n__detailed_school_page': u'More details for this school', u'lix_treasury_upload': u'B', u'fosList': [{u'link__fos_pivot': u'/search?search=&keywords=Science&sortCriteria=R&keepFacets=true&trk=prof-edu-field_of_study', u'fmt__fos_highlight': u'Science', u'name': u'Science', u'i18n__find_others': u'Find users with this keyword'}], u'enddate_iso': u'1988', u'fieldOfStudy': u'Science', u'link__peer_tracker_pivot_with_name': u'/college/?eduSchool=St%2E+Xavier%27s+School+Loyola+Hall&trk=prof-edu-school-name', u'educationId': 15978673, u'fmt__degree_highlight': u'H.S.C.', u'i18n__other_alumni': u"Find other members who attended St. Xavier's School Loyola Hall", u'startdate_iso': u'1976', u'schoolName': u"St. Xavier's School Loyola Hall", u'link__school_logo': u'/edu/school', u'lix_edu_profile_link': u'control', u'startdate_my': u'1976', u'enddate_my': u'1988', u'lix_edu_mvp_override': u'control'}

And I want to sort(ascending) the list on "enddate_iso" or "startdate_iso". Is it possible to do?

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
mickey
  • 23
  • 1
  • 1
  • 4

1 Answers1

0

Give it a try:

>>> from operator import itemgetter
>>> sorted(d8, key=itemgetter('startdate_iso'))

sorted produces a new list. If you want to sort in place, use sort():

>>> d8.sort(key=itemgetter('startdate_iso'))
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • "Give it a try" yet you give a solution? Or did you mean "Give this answer a try"? – TerryA Aug 21 '13 at 07:22
  • @Haidro the solution should work. I just hope that helps the OP to solve the problem :) – alecxe Aug 21 '13 at 07:31
  • @alecxe- The first solution worked. Thankyou :) The second solution provided throws a "TypeError: 'NoneType' object has no attribute '__getitem__'" – mickey Aug 21 '13 at 07:58
  • @mickey I'll bet that at some point accidentally assigned `None` to `d8`, then tried to do `sort` on that. Remember, the `.sort` method sorts *in place*, so you don't reassign it to the `d8` variable. – SethMMorton Aug 21 '13 at 08:05
  • @SethMMorton I'd suggest `d8` does actually contain a `None` value. If `d8` was `None` and you tried `sorted` you'd get a `TypeError` complaining it wasn't iterable, and if you tried `d8.sort()` you'd get an `AttributeError`... (although - having said that - not sure why the first/not the second works... probably something the OP is doing interactively) – Jon Clements Aug 21 '13 at 08:22
  • @JonClements Good catch... that's true. What confuses me then is that it worked with `sorted` and not with `.sort`... seems like if the list contained `None` both should fail with the `TypeError`. – SethMMorton Aug 21 '13 at 08:25