jquery - Sorting taking case into consideration -


i using following code

    function asc(a, b) {         return ($(b).text()) < ($(a).text());     } 

when pass a,d,c,e,b correct values a,b,c,d,e

however when pass a,d,c,e,b c,a,b,d,e

how can make code work when different case used?

if you're looking case-insensitive sort, easiest way convert both sides all-upper or all-lower case.

// note: not array#sort function asc(a, b) {     return $(b).text().tolowercase() < $(a).text().tolowercase(); } 

note, thought, i'm assuming you're not using array#sort, doesn't array#sort expects (return 0, <0, or >0).


side note: () around $(b).text() , $(a).text() in quoted code have no effect whatsoever, i've removed them above.


from comment:

if have sequence 'c' after 'c' in example, how it?

ah, that's different. works array#sort, you'll have modify match you're using asc (i don't know expect in case strings match): live copy

// note: designed array#sort, may need modifying function asc(a, b) {     var btext = $(b).text(),         atext = $(a).text(),         blc,         alc;      if (atext === btext) {         // same         return 0;     }     alc = atext.tolowercase();     blc = btext.tolowercase();     if (alc === blc) {         // case-insensitive match, compare case-sensitive ,         // ensure uppercase comes after lowercase.         return atext < btext ? 1 : -1;     }      // different ignoring case     return alc < blc ? -1 : 1; } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -