19

When I use sort($topics) I get something along the lines of:

  1. Apple
  2. Green
  3. Zebra
  4. grass

In this example, "grass" starts with a lower case g but ends up after "Zebra" which has a capital letter.

How do I make it so that it sorts it where it ignores whether the word starts with capitals or not?

  1. Apple
  2. Green
  3. grass
  4. Zebra
random
  • 9,774
  • 10
  • 66
  • 83
Simon Suh
  • 10,599
  • 25
  • 86
  • 110

2 Answers2

33

Call usort() as usort($topics, 'strnatcasecmp').

strcasecmp would do the job, too, but strnatcasecmp will also sort properly when you have numbers in your string.

apaderno
  • 28,547
  • 16
  • 75
  • 90
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
24

There is natcasesort .

natcasesort($topics);
xdazz
  • 158,678
  • 38
  • 247
  • 274