Jump to content

Sorting Arrays Non-case Sensitve


johnnyg24
 Share

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>

Edited by dsonesuk
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...