Mercurial > browser-couch
changeset 54:3607a6844691
Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 15 Apr 2009 08:01:14 -0700 |
parents | 08f868fc6e2b |
children | aa013581aeca |
files | browser-couch.js tests.js |
diffstat | 2 files changed, 16 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/browser-couch.js Tue Apr 14 17:35:22 2009 -0700 +++ b/browser-couch.js Wed Apr 15 08:01:14 2009 -0700 @@ -521,7 +521,6 @@ _MapView: function BC__MapView(mapResult) { var rows = []; var keyRows = []; - this.rows = rows; var mapKeys = mapResult.keys; var mapDict = mapResult.dict; @@ -530,13 +529,22 @@ var key = mapKeys[i]; var item = mapDict[key]; keyRows.push({key: key, pos: rows.length}); + var newRows = []; for (var j = 0; j < item.keys.length; j++) { var id = item.keys[j]; var value = item.values[j]; - rows.push({id: id, - key: key, - value: value}); + newRows.push({id: id, + key: key, + value: value}); } + newRows.sort(function(a, b) { + if (a.id < b.id) + return -1; + if (a.id > b.id) + return 1; + return 0; + }); + rows = rows.concat(newRows); } function findRow(key, keyRows) { @@ -552,6 +560,7 @@ return keyRows[0].pos; } + this.rows = rows; this.findRow = function MV_findRow(key) { return findRow(key, keyRows); };
--- a/tests.js Tue Apr 14 17:35:22 2009 -0700 +++ b/tests.js Wed Apr 15 08:01:14 2009 -0700 @@ -104,10 +104,10 @@ var expected = { rows:[{"id":"chunky","key":"dogen","value":1}, {"id":"monkey","key":"dude","value":1}, + {"id":"chunky","key":"hello","value":1}, {"id":"monkey","key":"hello","value":1}, - {"id":"chunky","key":"hello","value":1}, - {"id":"monkey","key":"there","value":1}, - {"id":"chunky","key":"there","value":1}] + {"id":"chunky","key":"there","value":1}, + {"id":"monkey","key":"there","value":1}] }; self.assertEqual(JSON.stringify(expected), JSON.stringify(result));