In the following demo script all works fine.
My question concerns the display order of the string array
when I try to ignore the CASE of the elements.
My question is highlighted in the script when you run it. ?
[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title>Sort Demos </title>
<style>
p { background-color: lime; }
b { background-color: orange; font-size: 1.4em; }
</style>
</head>
<body>
<h2>Sorting Numbers – sort() & sort().reverse()</h2>
<pre id=”sort1″></pre>
<h2>Sorting Numbers – sort(functions())</h2>
<pre id=”sort2″></pre>
<h2>Sorting Strings – sort() & sort().reverse() </h2>
<pre id=”sort3″></pre>
<pre id=”debug”></pre>
<script>
var nary = [1,10,100,200,20,2,5,50,1000],
sary = [‘Bee’,’bee’,’a’,’A’,’bonnett’,’Cat’,’cat’,’dog’,’zebra’,’Zebra’,’cash’,’fish’,’bird’,’devil’],
ary = [], str = ”, bkwd = ”, frwd = ”;
function d(IDS) { return document.getElementById(IDS); }
ary = [].concat(nary);
str = ‘orig: ‘+nary+’nnBkwd: ‘+nary.sort().reverse()+’nnFwrd: ‘+nary.sort();
d(‘sort1’).innerHTML = str;
ary = [].concat(nary);
bkwd = nary.sort( function(a,b){ return b-a; } ).join(‘,’);
frwd = nary.sort( function(a,b){ return a-b; } ).join(‘,’);
str = ‘orig: ‘+nary + ‘nnBkwd: ‘+bkwd + ‘nnFrwd: ‘+frwd;
d(‘sort2’).innerHTML = str;
ary = [].concat(sary);
bkwd = ary.sort().reverse().join(‘,’);
frwd = ary.sort().join(‘,’);
str = ‘orig: ‘+ sary + ‘nnBkwd: ‘+bkwd + ‘nnFrwd: ‘+frwd;
d(‘sort3’).innerHTML = str;
str = ‘<p>All of above work as expected</p><hr>n’;
ary = [].concat(sary);
bkwd = ary.sort( function(a,b) { return (a.toLowerCase() < b.toLowerCase()); } ).join(‘,’);
frwd = ary.sort( function(a,b) { return (a.toLowerCase() > b.toLowerCase()); } ).join(‘,’);
str += ‘<h2>Sorting Strings – sort(functions(toLowerCase())) – sort ignoring case</h2>’;
str += ‘Note order of duplicates in arrayn’;
str += ‘orig: ‘+ sary + ‘nnBkwd: ‘+bkwd + ‘nnFrwd: ‘+frwd;
[COLOR=”#FF0000″]
str += “<p>nWhy is ‘a’ before ‘A’ but ‘Bee’ is before ‘bee'”;
str += “nAND ‘Cat’ is before ‘cat’ but ‘zebra’ is before ‘Zebra'”
str += “nusing the <b>SAME</b> sort functions()n”;
str += “nShould A B C and Z be consistent before or after a b c and z?n </p>”;
[/COLOR]
document.getElementById(‘debug’).innerHTML = str;
</script>
</body>
</html>