[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
This commit is contained in:
Olivier Dony 2012-03-14 12:10:14 +01:00
parent 57b2b97308
commit 6a38a66d50
1 changed files with 19 additions and 12 deletions

View File

@ -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) {