[WebDialog] A-H, I-M, N-Z in Javascript
-
Can anyone help with an easy way to separate a 1000+ element array into alphabetical chunks as in the caption. First letters are both upper and lower case and there are also special characters. The last can be chucked into a "remainder" array I suppose.
Any help much appreciated!
Chris
-
Untested.
// assuming variable arr with the values we deal with var ah = new Array() var im = new Array() var nz = new Array() var others = new Array() arr.sort() for (value in arr) { switch(value.charAt(0)) { case 'a'; case 'b'; case 'c'; case 'd'; case 'e'; case 'f'; case 'g'; case 'h'; ah.push(value) break; case 'i'; case 'j'; case 'k'; case 'l'; case 'm'; im.push(value) break; case 'n'; case 'o'; case 'p'; case 'q'; case 'r'; case 's'; case 't'; case 'u'; case 'v'; case 'w'; case 'x'; case 'y'; case 'z'; nz.push(value) break; default; others.push(value) } }
-
@thomthom said:
Untested.
About to be! Thanks, will let you know when successful.
I need to add value.charAt(0).toLowerCase() I guess.
Positively
Chris
-
-
I wonder if javascripts sorting deals with unicode... wonder if it puts the norwegian æøå after z....
-
Sorry Thom I cannot get it to work yet. Can you spot anything silly in this code or guide me how to test "your bit." Takk
function apimMethodsAll(sl,xx){ //lists unique method names sl not used here; xx = "ah" and other labels var list = new Array(); for (n in nset) { nn = n.split("."); if(nn[0] > 10) continue; //cut as sample for testing - contains good range if(nn.length == 2){ thisName = window.nset[n].name; list.push(new Array(thisName,n)); } } list.sort(); //list tests OK var lastName = null; var unique = new Array(); for(a=0; a<list.length; a++){ if (list[a][0] != lastName){ unique.push(list[a][1]); } lastName = list[a][0]; } //unique tests OK var ah = new Array() var im = new Array() var nz = new Array() var spec = new Array() for (value in unique){ switch(value.charAt(0).toLowerCase()){ case 'a'; case 'b'; case 'c'; case 'd'; case 'e'; case 'f'; case 'g'; case 'h'; ah.push(value) break; case 'i'; case 'j'; case 'k'; case 'l'; case 'm'; im.push(value) break; case 'n'; case 'o'; case 'p'; case 'q'; case 'r'; case 's'; case 't'; case 'u'; case 'v'; case 'w'; case 'x'; case 'y'; case 'z'; nz.push(value) break; default; spec.push(value) } } //tested alert(ah) blank list = eval(xx) //list blank coreSliderList(list,0); //Doms the list }
-
A snap of the result of running unique array rather than list in the code above
-
Sorry just by sending the last stuff I think I have found the answer. Can I get back to you. Takk
-
I discussed this with Jim just now and we decided that it is better to include the full methods output for the beta version so interested parties can understand and improve the code holistically. But thanks for your help. Sometimes I'm a little too quick off the mark!
Chris
Advertisement