I am trying to sort an array.
I have an array of data, where each line represents a new index in the array. I need to sort it descending, by the first number.
My array:
7[^.^]username[^.^]idnumber[^.^]State[^.^]
13[^.^] username[^.^] idnumber[^.^] State[^.^]
9[^.^] username[^.^] idnumber[^.^] State[^.^]
19[^.^] username[^.^] idnumber[^.^] State[^.^]
ksort and asort kind of work, but it recognizes a “9” as being bigger than “81”, so it would return the following:
13[^.^] username[^.^] idnumber[^.^] State[^.^]
19[^.^] username[^.^] idnumber[^.^] State[^.^]
7[^.^]username[^.^]idnumber[^.^]State[^.^]
9[^.^] username[^.^] idnumber[^.^] State[^.^]
I know I need some kind of custom sort, but I’m not sure how. Thanks.
All PHP
sort()functions can take sort_flags as their second argument. Try usingSORT_NUMERIC:sort($myArray, SORT_NUMERIC);Or use
usort()for a custom sorting callback