Rakesh
Rakesh

Reputation: 82755

Sorting a list containing Dictionary in Python

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

Upvotes: 0

Views: 203

Answers (2)

hamstergene
hamstergene

Reputation: 24429

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

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

Upvotes: 2

lvc
lvc

Reputation: 35059

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())

Upvotes: 6

Related Questions