From 6a38a66d50154d5da6062a10dfe541771c3610ef Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Wed, 14 Mar 2012 12:10:14 +0100 Subject: [FIX] BufferedDataSet: avoid randomizing results when there are no sort_fields Array.sort is not necessarily stable, and sorting an array with an unstable sort will cause a randomization of the original order, which should have been preserved. See also: - http://ecma262-5.com/ELS5_Section_15.htm#Section_15.4.4.11 - http://code.google.com/p/v8/issues/detail?id=90 lp bug: https://launchpad.net/bugs/928389 fixed bzr revid: odo@openerp.com-20120314111014-1igxhrynu5557di5 --- addons/web/static/src/js/data.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'addons/web/static/src/js/data.js') diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index 8e200951eaa..d9cfc5c4002 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -741,18 +741,25 @@ openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({ : (v1 > v2) ? 1 : 0; }; - records.sort(function (a, b) { - return _.reduce(sort_fields, function (acc, field) { - if (acc) { return acc; } - - var sign = 1; - if (field[0] === '-') { - sign = -1; - field = field.slice(1); - } - return sign * compare(a[field], b[field]); - }, 0); - }); + // Array.sort is not necessarily stable. We must be careful with this because + // sorting an array where all items are considered equal is a worst-case that + // will randomize the array with an unstable sort! Therefore we must avoid + // sorting if there are no sort_fields (i.e. all items are considered equal) + // See also: http://ecma262-5.com/ELS5_Section_15.htm#Section_15.4.4.11 + // http://code.google.com/p/v8/issues/detail?id=90 + if (sort_fields.length) { + records.sort(function (a, b) { + return _.reduce(sort_fields, function (acc, field) { + if (acc) { return acc; } + var sign = 1; + if (field[0] === '-') { + sign = -1; + field = field.slice(1); + } + return sign * compare(a[field], b[field]); + }, 0); + }); + } completion.resolve(records); }; if(to_get.length > 0) { -- cgit v1.2.3