From a6b8ca70af6ff8447dcfbdc6d7099a538e1686ec Mon Sep 17 00:00:00 2001
From: cvette <vette.christian@gmail.com>
Date: Mon, 22 Dec 2014 18:45:39 +0100
Subject: [PATCH] fix sorting with undefined in memory connector

---
 lib/connectors/memory.js | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js
index c43e5093..386b6c14 100644
--- a/lib/connectors/memory.js
+++ b/lib/connectors/memory.js
@@ -348,9 +348,14 @@ Memory.prototype.all = function all(model, filter, callback) {
 
   function sorting(a, b) {
     for (var i = 0, l = this.length; i < l; i++) {
-      if (a[this[i].key] > b[this[i].key]) {
+
+      if(a[this[i].key] === undefined && b[this[i].key] === undefined) {
+        return 0;
+      }
+
+      if (b[this[i].key] === undefined || a[this[i].key] > b[this[i].key]) {
         return 1 * this[i].reverse;
-      } else if (a[this[i].key] < b[this[i].key]) {
+      } else if (a[this[i].key] === undefined || a[this[i].key] < b[this[i].key]) {
         return -1 * this[i].reverse;
       }
     }