Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save malero/418204 to your computer and use it in GitHub Desktop.

Select an option

Save malero/418204 to your computer and use it in GitHub Desktop.

Revisions

  1. Matt revised this gist Feb 14, 2011. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions python-sort-list-object-dictionary-multiple-key.4.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    from operator import itemgetter
    def multikeysort(items, columns, functions={}, getter=itemgetter, cmp=cmp):
    def multikeysort(items, columns, functions={}, getter=itemgetter):
    """Sort a list of dictionary objects or objects by multiple keys bidirectionally.
    Keyword Arguments:
    @@ -9,7 +9,6 @@ def multikeysort(items, columns, functions={}, getter=itemgetter, cmp=cmp):
    getter -- Default "getter" if column function does not exist
    operator.itemgetter for Dictionaries
    operator.attrgetter for Objects
    cmp -- The compare function to use
    """
    comparers = []
    for col in columns:
  2. malero revised this gist Jun 9, 2010. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion python-sort-list-object-dictionary-multiple-key.4.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    from operator import itemgetter
    def multikeysort(items, columns, functions={}, getter=itemgetter):
    def multikeysort(items, columns, functions={}, getter=itemgetter, cmp=cmp):
    """Sort a list of dictionary objects or objects by multiple keys bidirectionally.
    Keyword Arguments:
    @@ -9,6 +9,7 @@ def multikeysort(items, columns, functions={}, getter=itemgetter):
    getter -- Default "getter" if column function does not exist
    operator.itemgetter for Dictionaries
    operator.attrgetter for Objects
    cmp -- The compare function to use
    """
    comparers = []
    for col in columns:
  3. malero revised this gist May 29, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions python-sort-list-object-dictionary-multiple-key.3.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Sort name alphabetically and age in descending order
    mylist = sorted(mylist, key=lambda k: (k['name'].lower(), -k['age']))
  4. malero revised this gist May 29, 2010. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions python-sort-list-object-dictionary-multiple-key.2.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # Sort by multiple keys - case sensitive
    from operator import itemgetter
    mylist = sorted(mylist, key=itemgetter('name', 'age'))

    # Sort by multiple keys - case insensitive
    mylist = sorted(mylist, key=lambda k: (k['name'].lower(), k['age']))
  5. malero revised this gist May 29, 2010. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions python-sort-list-object-dictionary-multiple-key.1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # Sort a list of dictionary objects by a key - case sensitive
    from operator import itemgetter
    mylist = sorted(mylist, key=itemgetter('name'))

    # Sort a list of dictionary objects by a key - case insensitive
    mylist = sorted(mylist, key=lambda k: k['name'].lower())
  6. malero revised this gist May 29, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion python-sort-list-object-dictionary-multiple-key.5.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@

    # Sort by class and grade descending, case insensitive
    from operator import itemgetter, methodcaller
    # Create a unary function that will get the column value and then call the .lower() method on the value
    # Create a unary function that will get the column value and then call the .lower() method
    get_class = compose(itemgetter('class'), methodcaller('lower'))
    get_grade = compose(itemgetter('grade'), methodcaller('lower'))

  7. malero revised this gist May 29, 2010. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions python-sort-list-object-dictionary-multiple-key.5.py
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,4 @@
    get_class = compose(itemgetter('class'), methodcaller('lower'))
    get_grade = compose(itemgetter('grade'), methodcaller('lower'))

    ex1 = multikeysort(students, ['class', '-grade'],{'class':get_class,'grade':get_grade})



    ex1 = multikeysort(students, ['class', '-grade'],{'class':get_class,'grade':get_grade})
  8. malero revised this gist May 29, 2010. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions python-sort-list-object-dictionary-multiple-key.5.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    # We will use a list of dictionary objects representing students, their classes and their current grade
    students = [
    {'name':'Paul Allen', 'class':'Science','grade':'A'},
    {'name':'paul allen', 'class':'Math','grade':'C'},
    {'name':'Bob Lewis', 'class':'Science','grade':'D'},
    {'name':'Bob Lewis', 'class':'math','grade':'b'},
    {'name':'bob Lewis', 'class':'History','grade':'f'},
    ]

    # Sort by class and grade descending, case sensitive
    ex1 = multikeysort(students, ['class', '-grade'])


    # Sort by class and grade descending, case insensitive
    from operator import itemgetter, methodcaller
    # Create a unary function that will get the column value and then call the .lower() method on the value
    get_class = compose(itemgetter('class'), methodcaller('lower'))
    get_grade = compose(itemgetter('grade'), methodcaller('lower'))

    ex1 = multikeysort(students, ['class', '-grade'],{'class':get_class,'grade':get_grade})



  9. malero revised this gist May 29, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions python-sort-list-object-dictionary-multiple-key.4.py
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ def comparer(left, right):
    return sorted(items, cmp=comparer)

    def compose(inner_func, *outer_funcs):
    """Compose multiple unary functions together into a single unary function"""
    if not outer_funcs:
    return inner_func
    outer_func = compose(*outer_funcs)
  10. malero revised this gist May 29, 2010. 2 changed files with 3 additions and 1 deletion.
    Empty file removed gistfile1.txt
    Empty file.
    4 changes: 3 additions & 1 deletion python-sort-list-object-dictionary-multiple-key.4.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,9 @@ def multikeysort(items, columns, functions={}, getter=itemgetter):
    items -- A list of dictionary objects or objects
    columns -- A list of column names to sort by. Use -column to sort in descending order
    functions -- A Dictionary of Column Name -> Functions to normalize or process each column value
    getter -- operator.itemgetter for Dictionaries, operator.attrgetter for Objects
    getter -- Default "getter" if column function does not exist
    operator.itemgetter for Dictionaries
    operator.attrgetter for Objects
    """
    comparers = []
    for col in columns:
  11. malero revised this gist May 29, 2010. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions python-sort-list-object-dictionary-multiple-key.4.py
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,6 @@ def multikeysort(items, columns, functions={}, getter=itemgetter):
    columns -- A list of column names to sort by. Use -column to sort in descending order
    functions -- A Dictionary of Column Name -> Functions to normalize or process each column value
    getter -- operator.itemgetter for Dictionaries, operator.attrgetter for Objects
    Example:
    li = [{'fname':'Bob','lname':'Lewis'},{'fname':'bob','lname':'Louis'}]
    li = multikeysort(li, ['-lname', 'fname'])
    """
    comparers = []
    for col in columns:
  12. malero revised this gist May 29, 2010. 2 changed files with 12 additions and 0 deletions.
    Empty file added gistfile1.txt
    Empty file.
    12 changes: 12 additions & 0 deletions python-sort-list-object-dictionary-multiple-key.4.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,17 @@
    from operator import itemgetter
    def multikeysort(items, columns, functions={}, getter=itemgetter):
    """Sort a list of dictionary objects or objects by multiple keys bidirectionally.
    Keyword Arguments:
    items -- A list of dictionary objects or objects
    columns -- A list of column names to sort by. Use -column to sort in descending order
    functions -- A Dictionary of Column Name -> Functions to normalize or process each column value
    getter -- operator.itemgetter for Dictionaries, operator.attrgetter for Objects
    Example:
    li = [{'fname':'Bob','lname':'Lewis'},{'fname':'bob','lname':'Louis'}]
    li = multikeysort(li, ['-lname', 'fname'])
    """
    comparers = []
    for col in columns:
    column = col[1:] if col.startswith('-') else col
  13. malero created this gist May 29, 2010.
    23 changes: 23 additions & 0 deletions python-sort-list-object-dictionary-multiple-key.4.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    from operator import itemgetter
    def multikeysort(items, columns, functions={}, getter=itemgetter):
    comparers = []
    for col in columns:
    column = col[1:] if col.startswith('-') else col
    if not column in functions:
    functions[column] = getter(column)
    comparers.append((functions[column], 1 if column == col else -1))

    def comparer(left, right):
    for func, polarity in comparers:
    result = cmp(func(left), func(right))
    if result:
    return polarity * result
    else:
    return 0
    return sorted(items, cmp=comparer)

    def compose(inner_func, *outer_funcs):
    if not outer_funcs:
    return inner_func
    outer_func = compose(*outer_funcs)
    return lambda *args, **kwargs: outer_func(inner_func(*args, **kwargs))