-2

If have an array like this

var tab = ['1185 Design','3 D Exhibits','44Doors', '4Concepts','ABC Data','acceleration'];

I want to sort it so that small letter 'a' element comes before capital letter 'A' element. Thanks.

user3555971
  • 57
  • 1
  • 10
  • See [*How does sort function work in JavaScript, along with compare function*](http://stackoverflow.com/questions/6567941/how-does-sort-function-work-in-javascript-along-with-compare-function). – RobG Jun 07 '16 at 06:48
  • And should "zebra" come before "Antelope"? (Your example only has the same word with different capitalisation.) – nnnnnn Jun 07 '16 at 06:49
  • 1
    Possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – just_user Jun 07 '16 at 06:50
  • I want a-z to come first before A-Z – user3555971 Jun 07 '16 at 08:28

1 Answers1

2

Your array :

var tab = ["Animal","123",  "animal"]

Sort function :

tab.sort(function(a,b){return a.localeCompare(b); })

OUTPUT :

["123", "animal", "Animal"]

DEMO HERE

Jax Teller
  • 1,447
  • 2
  • 15
  • 24
  • Hi , I need it like a-z comes first then A-Z example : apple,APPLE,bat ,Button – user3555971 Jun 07 '16 at 08:22
  • How you want it sort your exemple ? apple,APPLE,bat ,Button – Jax Teller Jun 07 '16 at 08:34
  • You want apple, bat, APPLE, Button ? – Jax Teller Jun 07 '16 at 08:35
  • No , a A together then b B like this apple,APPLE,bat ,Button .. always small letter before capital – user3555971 Jun 07 '16 at 08:38
  • My code does it... I don't understand what's the problem – Jax Teller Jun 07 '16 at 08:41
  • if I have these elements 1185 Design,3 D Exhibits,44Doors,4Concepts,ABC Data,acceleration,Accent Electronic,Acme Computer Components,Acorn Marketing,Ad verum,Adacado,Adobe Systems,AdPeopleWorldWide,Advania,Affiniti,Afghanistan,Agua Marketing,aimClear,Albania,Algeria,Allbound,AMD,AMI Communications,Amsterdam Worldwide,Andorra,Angola,Anguilla,Aniden Interactive . that acceleration element should come before 'A' element – user3555971 Jun 07 '16 at 08:42
  • I tried your code on the above example small letter acceleration element is not coming before ABC Data element – user3555971 Jun 07 '16 at 08:45