Page 1 of 1

sorting numerically with strings possible?

Posted: Tue Jun 03, 2008 4:12 pm
by kryles
Hi, I have a list of random numbers I want listed numerically but some of them contain alphabetical characters. I tried sort($array, SORT_NUMERICAL); and sort($array, SORT_STRING); but neither gave me what I was hoping for.

here is a sample

Code: Select all

 
 
$boItems = "0207000000,1000WH420C,1188000000,1801WH40BD,0229US0000,1000WH480C,1190000000,1803BE48DF,0306003800,1000WH500C,1204020000,91950040DD,
0307003800,1006PW360D,1207LLMM00,919500440F,0333000000,1006PW420D,1207MM0000,919502380D,0606000000,1006PW440F,1227000000,919502440C,
0607000000,1006PW44DD,1238000000,1801WH38BD,1182RE0200,0631000000,1801WH36BD,1182QE0200,0623MM0000,1801CF40BD,1142000000,0623LLMM00,    1801CF38DF,1109000000,0622BL0000,126502480C,1056000000,0621010000,126502440C,1048000000,0616000000,0610000000,1012000000,126502380C";
 
$boArray = explode(',',$boItems);
 
sort($boArray);
 
 
I'll show you my results using the different sorts and what I am hoping for

Code: Select all

 
Sort($array, SORT_STRING)
 
0307003800
0607000000
1801CF38DF
0207000000
0229US0000
0306003800
0333000000
0606000000
0610000000
0616000000
0621010000
0622BL0000
 

Code: Select all

 
sort($array)
 
1801CF38DF
0207000000
0306003800
0307003800
0333000000
0606000000
0607000000
0229US0000
0610000000
 

Code: Select all

 
sort($array, SORT_NUMERIC)
0229US0000
0622BL0000
0623MM0000
0623LLMM00
1000WH500C
1000WH480C
1000WH420C
1207LLMM00
1803BE48DF
91950040DD
126502380C
126502440C
126502480C
0207000000
 
What I want is the following

Code: Select all

 
 
0207000000 
0229US0000
0306003800
0307003800
0607000000
0623LLMM00
1000WH420C      
1188000000 
126502380C 
1801WH36BD          
919500440F
 
I hope this is making sense, lol.

Thanks,

Re: sorting numerically with strings possible?

Posted: Tue Jun 03, 2008 5:19 pm
by nowaydown1
I think that your sort is going wacky because you have whitespace in your string since it's multiline, which eventually winds up in your explode. Try this:

Code: Select all

 
<?php
 
$boItems = "0207000000,1000WH420C,1188000000,1801WH40BD,0229US0000,1000WH480C,1190000000,1803BE48DF,0306003800,1000WH500C,1204020000,91950040DD,
0307003800,1006PW360D,1207LLMM00,919500440F,0333000000,1006PW420D,1207MM0000,919502380D,0606000000,1006PW440F,1227000000,919502440C,
0607000000,1006PW44DD,1238000000,1801WH38BD,1182RE0200,0631000000,1801WH36BD,1182QE0200,0623MM0000,1801CF40BD,1142000000,0623LLMM00,    1801CF38DF,1109000000,0622BL0000,126502480C,1056000000,0621010000,126502440C,1048000000,0616000000,0610000000,1012000000,126502380C";
 
$boArray = explode(',',$boItems);
 
foreach($boArray as $value) {
    $sortArray[] = trim($value);
}
 
sort($sortArray);
 
echo "<pre>";
print_r($sortArray);
 
?>
 

Re: sorting numerically with strings possible? SOLVED

Posted: Wed Jun 04, 2008 7:19 am
by kryles
Genius :drunk:

That worked perfectly, I was thinking whitespace isn't a factor in c/c++/PHP but of course it does inside the string lol.

Much appreciated

Re: sorting numerically with strings possible?

Posted: Wed Jun 04, 2008 7:31 am
by Kieran Huggins
http://ca.php.net/natsort

may also be of some interest

Re: sorting numerically with strings possible?

Posted: Wed Jun 04, 2008 7:48 am
by kryles
dang, yah that works perfectly also. Surprised I looked through all the functions I saw with sort but totally missed that one heh

Thanks, good to know :mrgreen: