Jump to content

Sorting Arrays Non-case Sensitve


johnnyg24

Recommended Posts

I am looking for a way to sort() an array from a-z. The problem I am running into is when an array element begins with a lowercase letter, it is being pushed to the bottom of the stack. I can't use toUpperCase() or toLowerCase() because the array element needs to appear on the page as the user has entered it. Is there a non-case sensitive sort() function?

Link to comment
Share on other sites

this is an old method i used, a long time ago<script type="text/javascript">/*<![CDATA[*//*---->*/originalarray = new Array("a","b","c","F","g","A","B","h","j","k","L","M","D","E");arraylen = originalarray.length;for(i=0;i<arraylen;i++) { for(j=1;j<arraylen;j++) { if(originalarray.toLowerCase() < originalarray[j].toLowerCase()) { tmp=originalarray originalarray = originalarray[j]; originalarray[j]= tmp; } }}for(j=1;j<arraylen;j++) { document.write(originalarray[j]+", "); }/*--*//*]]>*/</script>

Link to comment
Share on other sites

You can also give the sort method your own comparison function to use:

function compare_insensitive(a, b){  a = new String(a);  b = new String(b);  if (a.toLowerCase() < b.toLowerCase())	 return -1;  if (a.toLowerCase() > b.toLowerCase())	 return 1;  // a must be equal to b  return 0;}ar.sort(compare_insensitive);

https://developer.mozilla.org/en/Core_JavaS...ects/Array/sort

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...