list - How can I sort numbers from high to low followed by letters in PHP? -
basically need sort list can contain either years or words, , have put recent years @ top, , words below of years.
so if list contains 2012,2013,2014,apple,banana,couch; sorted :
2014, 2013, 2012, apple, banana, couch
how done?
there lot of built-in php sorting functions, won't cover all weird edge cases. example, almost use simple natsort()
if weren't sorting in different directions. kinds of custom sorts 1 want (by numbers descending, words ascending) may best off writing own simple sort comparison function!
so check out usort()
http://php.net/manual/en/function.usort.php that.
essentially, pass array , function write returns 1, 0, or -1 depending on desired comparison of 2 sort elements, , you'll out sorted version of array.
alternatively, may want sort data coming out of database first, that's faster.
Comments
Post a Comment