From c60e37d88c95f4e790a679f30698392681a7602d Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 22 Jul 2015 09:55:36 +0930 Subject: [PATCH] Use stable sort for item lists --- framework/core/js/lib/utils/ItemList.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/framework/core/js/lib/utils/ItemList.js b/framework/core/js/lib/utils/ItemList.js index 1adb3687f..215c1c765 100644 --- a/framework/core/js/lib/utils/ItemList.js +++ b/framework/core/js/lib/utils/ItemList.js @@ -19,7 +19,7 @@ export default class ItemList { * priority will be positioned before items with a lower priority. * @public */ - add(key, content, priority) { + add(key, content, priority = 0) { this[key] = new Item(content, priority); } @@ -52,10 +52,18 @@ export default class ItemList { if (this.hasOwnProperty(i) && this[i] instanceof Item) { this[i].content.itemName = i; items.push(this[i]); + this[i].key = items.length; } } - return items.sort((a, b) => b.priority - a.priority).map(item => item.content); + return items.sort((a, b) => { + if (a.priority === b.priority) { + return a.key - b.key; + } else if (a.priority > b.priority) { + return -1; + } + return 1; + }).map(item => item.content); } }