CODE: Generic Sort Routine

Discussion in 'Python' started by Kamilche, Aug 19, 2004.

  1. Kamilche

    Kamilche Guest

    I've written a generic sort routine that will sort dictionaries,
    lists, or tuples, either by a specified key or by value.

    Comments welcome!

    import types

    def sort(container, key = None, ascending = True):
    ' Sort lists or dictionaries by the specified key'
    t = type(container)
    if t in (types.ListType, types.TupleType):
    thelist = container
    else:
    thelist = container.values()
    if key:
    sorted = zip([getattr(item, key) for item in thelist],
    thelist)
    else:
    sorted = list(thelist)
    sorted.sort()
    if ascending == False:
    sorted.reverse()
    if key:
    return [x[1] for x in sorted]
    else:
    return sorted

    class simple:
    name = 'simple'
    def __init__(self, word):
    self.word = word
    def __repr__(self):
    return str(self.__dict__)

    def test():
    # Set up the test data
    line = 'now is the time for all good men to come to the aid of
    their country.'
    words = line.split(' ')
    compareto = line.split(' ')
    compareto.sort()
    assert words != compareto
    objects = []
    for word in words:
    o = simple(word)
    objects.append(o)

    # Sort a list of words
    test1 = sort(words)
    assert test1 == compareto

    # Sort a list of objects
    lst = sort(objects, 'word')
    test2 = []
    for item in lst:
    test2.append(item.word)
    assert(test2 == compareto)

    # Sort a tuple of objects
    tup = tuple(objects)
    lst = sort(tup, 'word')
    test3 = []
    for item in lst:
    test3.append(item.word)
    assert(test3 == compareto)

    # Sort a dict of objects
    d = {}
    ctr = 1
    for item in objects:
    d[ctr] = item
    ctr += 1
    lst = sort(d, 'word')
    test4 = []
    for item in lst:
    test4.append(item.word)
    assert(test4 == compareto)

    # Reverse sort a dict of objects
    d = {}
    ctr = 1
    for item in objects:
    d[ctr] = item
    ctr += 1
    lst = sort(d, 'word', False)
    test4 = []
    for item in lst:
    test4.append(item.word)
    compareto.reverse()
    assert(test4 == compareto)

    # Sort a list of numbers
    lst = [5, 9, 3, 54, 6, 65.7, 2.4, 9999]
    lst = sort(lst)
    print lst

    # Done
    print "All tests passed!"

    if __name__ == "__main__":
    test()
    Kamilche, Aug 19, 2004
    #1
    1. Advertising

  2. Kamilche wrote:

    > I've written a generic sort routine that will sort dictionaries,
    > lists, or tuples, either by a specified key or by value.
    >
    > Comments welcome!


    ....snip...
    > if key:
    > sorted = zip([getattr(item, key) for item in thelist],
    > thelist)
    > else:
    > sorted = list(thelist)

    ....snip...

    I'd change that 'if key:' line to 'if key is not None:', so that you could
    sort on other non-True values.

    Andy
    Andrew Henshaw, Aug 23, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rkk
    Replies:
    9
    Views:
    801
    CBFalconer
    Sep 24, 2006
  2. Tom Carter

    Sort Routine Problem

    Tom Carter, Jan 19, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    137
    Tom Carter
    Jan 20, 2004
  3. PatLaf

    AspNet DataGrid won't fire sort routine

    PatLaf, Oct 29, 2003, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    171
    Alvin Bruney
    Oct 30, 2003
  4. Navin
    Replies:
    1
    Views:
    668
    Ken Schaefer
    Sep 9, 2003
  5. Arthur

    Perl Sort Routine

    Arthur, Feb 12, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    85
    Paul Lalli
    Feb 12, 2004
Loading...

Share This Page