Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

case-insensitive array sort 1

Status
Not open for further replies.

peterpann

Programmer
Jun 19, 2007
63
GB
How can I perform a case-insensitive array sort, resulting in a,A,Z,z or a,A,z,Z rather than A,Z,a,z ?
 
Hi

Read about [tt]Array.sort()[/tt] in a JavaScript reference. Then write a comparator function :
JavaScript:
[teal][[/teal][green][i]'qwe'[/i][/green][teal],[/teal][green][i]'asd'[/i][/green][teal],[/teal][green][i]'zxc'[/i][/green][teal],[/teal][green][i]'QWE'[/i][/green][teal],[/teal][green][i]'ASD'[/i][/green][teal],[/teal][green][i]'ZXC'[/i][/green][teal]].[/teal][COLOR=darkgoldenrod]sort[/color][teal]([/teal][b]function[/b][teal]([/teal]a[teal],[/teal]b[teal])[/teal] [teal]{[/teal]
  [b]var[/b] al[teal]=[/teal]a[teal].[/teal][COLOR=darkgoldenrod]toLowerCase[/color][teal](),[/teal]bl[teal]=[/teal]b[teal].[/teal][COLOR=darkgoldenrod]toLowerCase[/color][teal]()[/teal]
  [b]return[/b] al[teal]==[/teal]bl[teal]?([/teal]a[teal]==[/teal]b[teal]?[/teal][purple]0[/purple][teal]:[/teal]a[teal]<[/teal]b[teal]?-[/teal][purple]1[/purple][teal]:[/teal][purple]1[/purple][teal]):[/teal]al[teal]<[/teal]bl[teal]?-[/teal][purple]1[/purple][teal]:[/teal][purple]1[/purple]
[teal]}[/teal][teal])[/teal]

[gray]// returns : ["ASD", "asd", "QWE", "qwe", "ZXC", "zxc"][/gray]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top