Add Array.stableSort from mootools forge.

Change calls to Array.sort to use new Array.stableSort. Fixes sorting problems on Chrome
This commit is contained in:
Kate von Roeder
2013-11-20 05:47:36 -08:00
parent cec88319fe
commit f865484182
3 changed files with 64 additions and 7 deletions
@@ -34,6 +34,7 @@ class ClientScript(Plugin):
'scripts/library/question.js',
'scripts/library/scrollspy.js',
'scripts/library/spin.js',
'scripts/library/Array.stableSort.js',
'scripts/couchpotato.js',
'scripts/api.js',
'scripts/library/history.js',
@@ -0,0 +1,56 @@
/*
---
script: Array.stableSort.js
description: Add a stable sort algorithm for all browsers
license: MIT-style license.
authors:
- Yorick Sijsling
requires:
core/1.3: '*'
provides:
- [Array.stableSort, Array.mergeSort]
...
*/
(function() {
var defaultSortFunction = function(a, b) {
return a > b ? 1 : (a < b ? -1 : 0);
}
Array.implement({
stableSort: function(compare) {
// I would love some real feature recognition. Problem is that an unstable algorithm sometimes/often gives the same result as an unstable algorithm.
return (Browser.chrome || Browser.firefox2 || Browser.opera9) ? this.mergeSort(compare) : this.sort(compare);
},
mergeSort: function(compare, token) {
compare = compare || defaultSortFunction;
if (this.length > 1) {
// Split and sort both parts
var right = this.splice(Math.floor(this.length / 2)).mergeSort(compare);
var left = this.splice(0).mergeSort(compare); // 'this' is now empty.
// Merge parts together
while (left.length > 0 || right.length > 0) {
this.push(
right.length === 0 ? left.shift()
: left.length === 0 ? right.shift()
: compare(left[0], right[0]) > 0 ? right.shift()
: left.shift());
}
}
return this;
}
});
})();
+7 -7
View File
@@ -111,6 +111,10 @@ Page.Settings = new Class({
Cookie.write('advanced_toggle_checked', +self.advanced_toggle.checked, {'duration': 365});
},
sortByOrder: function(a, b){
return (a.order || 100) - (b.order || 100)
},
create: function(json){
var self = this;
@@ -141,13 +145,11 @@ Page.Settings = new Class({
options.include(section);
});
options.sort(function(a, b){
return (a.order || 100) - (b.order || 100)
}).each(function(section){
options.stableSort(self.sortByOrder).each(function(section){
var section_name = section.section_name;
// Add groups to content
section.groups.sortBy('order').each(function(group){
section.groups.stableSort(self.sortByOrder).each(function(group){
if(group.hidden) return;
if(self.wizard_only && !group.wizard)
@@ -184,9 +186,7 @@ Page.Settings = new Class({
}
// Add options to group
group.options.sort(function(a, b){
return (a.order || 100) - (b.order || 100)
}).each(function(option){
group.options.stableSort(self.sortByOrder).each(function(option){
if(option.hidden) return;
var class_name = (option.type || 'string').capitalize();
var input = new Option[class_name](section_name, option.name, self.getValue(section_name, option.name), option);