From 73e2ba991c0e74654f3c2e2ae02485196d25fe1e Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Wed, 6 Dec 2017 13:51:42 +0100 Subject: [FIX] web: add a stableSort method for sorting x2m Issue: create 60 lines in ordered x2many with the same value (0) for the order field and make a change ; the displayed records will be switched. The problem appears because the sorting function is not stable and the records with the same values can change place (hence change page), which is quite disturbing for the user. --- addons/web/static/src/js/core/utils.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'addons/web/static/src/js/core/utils.js') diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js index ac9310dce90..acf8ca4d68d 100644 --- a/addons/web/static/src/js/core/utils.js +++ b/addons/web/static/src/js/core/utils.js @@ -348,6 +348,23 @@ var utils = { 'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString() ].join(';'); }, + /** + * Sort an array in place, keeping the initial order for identical values. + * + * @param {Array} array + * @param {function} iteratee + */ + stableSort: function (array, iteratee) { + var stable = array.slice(); + return array.sort(function stableCompare (a, b) { + var order = iteratee(a, b); + if (order !== 0) { + return order; + } else { + return stable.indexOf(a) - stable.indexOf(b); + } + }); + }, /** * @param {any} array * @param {any} elem1 -- cgit v1.2.3