Widget talk:Map bonus rewards/script.js

From Guild Wars 2 Wiki
Jump to navigationJump to search

Sort buttons don't work in chrome[edit]

    $('#sort-alpha').click(function(){
        $('.weekcontainer').each(function() {
            $('.moreinfo',this).sort(function (a, b) {
               return $(a).attr('data-zone') > $(b).attr('data-zone');
            }).appendTo(this);
        });
    });
    $('#sort-value').click(function(){
        $('.weekcontainer').each(function() {
            $('.moreinfo',this).sort(function (a, b) {
               return $(b).attr('data-sort-value') - $(a).attr('data-sort-value');
            }).appendTo(this);
        });
    });

What am I missing here, why doesn't this work? -Chieftain AlexUser Chieftain Alex sig.png 20:42, 18 January 2022 (UTC)

Ok, so i looked into it and it seems like using > as a means to compare values in sort with is something that chrome doesn't like. Compare e.g. the behaviour of the following in chrome (won't sort) and e.g. firefox (will sort):
["b", "a", "c"].sort(function (a, b) { // Same thing holds for $([...]).sort and or [2, 1, 3], etc. too.
	return a > b;
});
// = [ "a", "b", "c" ] (In e.g. firefox.)
// = [ "b", "a", "c" ] (In chrome.)
Compare this again against the following, which will sort propperly (with the help of a function used in Widget:Daily achievements currently already) in both browsers that are compared as example above:
function compareStrings(a, b) {
    var c;
    for (var i=0,l=Math.min(a.length, b.length);i < l;i++) {
        c = a.charCodeAt(i)-b.charCodeAt(i);
        if (c) {
            return Math.sign(c);
        }
    }
    return Math.sign(a.length-b.length);
}
["b", "a", "c"].sort(function (a, b) {
	return compareStrings(a, b);
});
// = [ "a", "b", "c" ] (In both firefox and chrome.)
Given this i'd suggest the replacement of the > with the above function or similar. Nightsky (talk) 07:07, 19 January 2022 (UTC)