changeset 134:b6242f916cef

stub; needs a test
author Jeff Hammel <k0scist@gmail.com>
date Tue, 17 Mar 2015 11:38:43 -0700
parents 8b120c7f0cf9
children 12649a88545c
files numerics/sort.py
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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)