20

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
1
  • Why does gre come before gra? Commented Apr 1, 2024 at 5:50

2 Answers 2

36

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.

Sign up to request clarification or add additional context in comments.

Comments

25

There is natcasesort .

natcasesort($topics);

1 Comment

note: natcasesort sorts special characters (dots, underscores...) differently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.