When I use sort($topics)
I get something along the lines of:
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?
The natcasesort() function sorts an array by using a "natural order" algorithm. The values keep their original keys.
Approach: The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With