views:

68

answers:

1

HI, i need to order a list in PHP that looks like: B1200 120A81 00A12 00A22 C100B C100C

ordered list would be: 00A12 00A22 120A81 B1200 C100B C100C

I was thinking about splitting each line in multidimensional arrays and order it but i am stuck and maybe theres a completely different way for that.

Thanks!

+6  A: 

If the normal sort function will do what you want then splitting/sorting it would be easy:

// break up the string into an array on spaces
$new_array = explode(' ', $input);
// sort the array
sort($new_array);
// put the string back together
$sorted_string = implode(' ', $new_array);

or, more succinctly:

$sorted_string = implode(' ', sort(explode(' ', $input)));

If the default sort() won't give you what you want you should check out the usort() function.

fiXedd
I'd like to add, that you may even use the "natsort" function.
FlorianH
Use sort with the SORT_NUMERIC flag: sort($new_array, SORT_NUMERIC);# SORT_REGULAR - compare items normally (don't change types)# SORT_NUMERIC - compare items numerically# SORT_STRING - compare items as strings# SORT_LOCALE_STRING - compare items as strings
powtac
SORT_NUMERIC is wrong in this case (not all digits, also letters). SORT_STRING is the correct one. If the hex strings can be mixed case you want to convert them to all upper or all lower before sorting (maybe even splitting)
ptor