# HG changeset patch # User Jeff Hammel # Date 1426617523 25200 # Node ID b6242f916cef162c24774d0fb897efe145c98a79 # Parent 8b120c7f0cf91c3f445b36015fb494cda39b9b77 stub; needs a test diff -r 8b120c7f0cf9 -r b6242f916cef numerics/sort.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numerics/sort.py Tue Mar 17 11:38:43 2015 -0700 @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +sort columned data +""" + +# module globals +__all__ = ['Sorter'] + +class Sorter(object): + """ + sorter for columned data + """ + + def __init__(self, *indices): + """ + indices -- 2-tuple of (index, forward) + where forward should be True or False + """ + self.indices = indices + + def __call__(self, rows): + return sorted(rows, key=self.key) + + def key(self, row): + retval = [] + for index, forward in self.indices: + value = row[index] + if not forward: + value = -value + retval.append(value) + return tuple(retval)