0

I have list that contains dictionary that need to be sorted based on the alphabetic order

[
    {
        'index': False,
        'definition': {
            'id': 1111111L,
            'value': u'Large Content'
        },
        'id': 1234567L,
        'name': {
            'id': 9999999999L,
            'value': u'INTRODUCTION'
        }
    },
    {
        'index': False,
        'definition': {
            'id': 22222222L,
            'value': u'Large Content'
        },
        'id': 2L,
        'name': {
            'id': 3333333333333l,
            'value': u'Abstract'
        }
    },
    {
        'index': False,
        'definition': {
            'id': 8888888888L,
            'value': u'Large Content'
        },
        'id': 1L,
        'name': {
            'id': 343434343434L,
            'value': u'Bulletin'
        }
    }
    {
        'index': False,
        'definition': {
            'id': 1113311L,
            'value': u'Large Content'
        },
        'id': 333434L,
        'name': {
            'id': 9999999999L,
            'value': u'<b>END</b>'
        }
    },
] 

I need to sort based on ['name']['value'] to result

Abstract
Bulletin
INTRODUCTION
END

But when i do it i get the capital letters first

bg = []
for n in a:
   bg = sorted(a, key=lambda n: n["name"]["value"])

INTRODUCTION
END
Abstract
Bulletin
3
  • I can't reproduce your results. For the data you have given, I get [u'<b>END</b>', u'Abstract', u'Bulletin', u'INTRODUCTION']. Commented May 22, 2012 at 7:07
  • I remove the html tags using the tip from love-python.blogspot.com/2008/07/… Commented May 22, 2012 at 7:26
  • I just meant that your example doesn't show the problem you were having. In fact, for those four words, case-sensitivity makes no difference. Commented May 22, 2012 at 8:03

2 Answers 2

6

To make the sort case insensitive, put everything in lower case in your sort key:

bg = sorted(a, key=lambda n: n["name"]["value"].lower())
Sign up to request clarification or add additional context in comments.

Comments

2

Because capitals are lexicographically smaller. Drop letter case before sorting:

key=lambda n: n["name"]["value"].lower()

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.