sortfunction test in IE, Firefox and etc when Array's elements are String.

1. order asc

test code

        var arr = new Array('1M','10K','1G','102K','85M','23M','133K','626B','919M','19K');
        
        document.write(arr.sort(function(a,b){
            var sa = new String(a);     //local string variables here is very important especially in IE
            var sb = new String(b);
            sa = sa.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            sb = sb.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            return parseInt(sa) - parseInt(sb);

            /** this won't work in IE but Firefox
            a = a.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            b = b.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            return parseInt(a) - parseInt(b);
            */
        }).join('\n'));
        

execute result

2. order desc

test code

        document.write(arr.sort(function(a,b){
            var sa = new String(a);
            var sb = new String(b);

            sa = sa.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            sb = sb.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            return parseInt(sb) - parseInt(sa);     //here to change order by

            /**  this won't work in IE but Firefox
            a = a.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            b = b.replace(/(\.(\d+))?G/ig,'$2000000000').replace(/(\.(\d+))?M/ig,'$2000000').replace(/(\.(\d+))?K/ig,'$2000');
            return parseInt(b) - parseInt(a);
            */
        }).join('\n'));
        

execute result

<<Back home  Naked(no css) site by Lagtan#gmail.com.