Repository: avilum/linqit

Issue:
Add support for order_by?
Hey, great library. I would love to do something like this but seems to just use the substandard built-in list implementation which returns None:

```python
people = List()
people.append(Person("Michael", 47))
people.append(Person("Sarah", 50))
people.append(Person("Jake", 42))

older = people.where(lambda p: p.age > 43).sort(lambda p: -p.age)
```

But sadly, sort returns None and I didn't see an `order_by`, what am I missing?

Discussion:

Patch:
diff --git a/linqit/__init__.py b/linqit/__init__.py
index 05260a1..af20d43 100644
--- a/linqit/__init__.py
+++ b/linqit/__init__.py
@@ -3,7 +3,7 @@ from itertools import chain
 
 DEFAULT_LAZY = True
 
-# A default variable for the fucntion, so None as an argument will be valid, but not default.
+# A default variable for the function, so None as an argument will be valid, but not default.
 _NONE = type('_NONE', (object,), {})
 
 
@@ -202,6 +202,13 @@ class List(list):
                 return default
             raise
 
+    def order_by(self, expression=None):
+        """
+        Returns a List of data, sorted according to expression. If no expression is given, the default sort is used.
+        """
+        sorted_data = sorted(self, key=expression)
+        return List(sorted_data)
+
     def select(self, expression):
         """
         Returns a list of the values of the expression, from all the wrapped elements.


Test patch:
diff --git a/tests/test_list.py b/tests/test_list.py
index ddafe69..ff3e49a 100644
--- a/tests/test_list.py
+++ b/tests/test_list.py
@@ -1,5 +1,6 @@
 from datetime import datetime
 from unittest import TestCase
+
 from linqit import List
 
 
@@ -213,3 +214,22 @@ class ListTests(TestCase):
         self.assertEqual(self.list[1].name, self.list.concat(self.list).where(lambda e: e.age > 18).skip(1).except_for(
             lambda e: e.name == 'bob').select(
             lambda x: x.name).last())
+
+    def test_order_by_bare(self):
+        data = [1, -1, 7, 200, 4, 3]
+        sorted_data = sorted(data)
+
+        linq_data = List(data).order_by()
+        self.assertEqual(sorted_data, linq_data)
+
+    def test_order_by_complex(self):
+        data = [
+            Person('jake', 'samson', None, 32),
+            Person('sam', 'thompson', None, 44),
+            Person('sarah', 'smith', None, 41),
+            Person('zoe', 'lee', None, 27),
+        ]
+        sorted_data = sorted(data, key=lambda p: p.age, reverse=True)
+
+        linq_data = List(data).order_by(lambda p: -p.age)
+        self.assertEqual(sorted_data, linq_data)