Matt79 Posted January 19, 2010 Share Posted January 19, 2010 I have created a page that I place all of the acronyms that I come across. I have them sorted in alphabetical order using the sort command. It sorts most of them correctly, but there are some that are in incorrect places. I have tried deleting the ones that are in the wrong place, but then new ones appear in their place. If anyone could give me some help that would be great. :-) Below is the code that I created. I have all of the acronyms contained in the $raw_data variable. $split=explode("1234567890",$raw_data); sort($split); $num=0; $count=count($split); while($num < $count) { echo $split[$num].'<br/>'; $num++; } Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/ Share on other sites More sharing options...
oni-kun Posted January 19, 2010 Share Posted January 19, 2010 It would help to get a sample of the $raw_data variable and as well the obvious problem entries (that you claim are in the wrong places). It's hard to predict an error, Without the error.. Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-997852 Share on other sites More sharing options...
Matt79 Posted January 19, 2010 Author Share Posted January 19, 2010 Ok you can see how it is being sorted here http://snyderit.info/test/acc.php How I have the $raw_data setup is like this: <?php $raw_data=" customer premises equipment (CPE)1234567890 Service provider (SP)1234567890 virtual circuits (VC)1234567890 Point to Point Tunneling Protocol (PPTP)1234567890 Layer 2 Tunneling Protocol (L2TP)1234567890 IP security (IPsec)1234567890"; $split=explode("1234567890",$raw_data); sort($split); $num=0; $count=count($split); while($num < $count) { echo $split[$num].'<br/>'; $num++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998109 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 Try using natsort() to sort your array. That should fix it. Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998118 Share on other sites More sharing options...
Matt79 Posted January 19, 2010 Author Share Posted January 19, 2010 That changes the way it is sorted, but it is much worse using natsort(). It almost appears random. Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998155 Share on other sites More sharing options...
laffin Posted January 19, 2010 Share Posted January 19, 2010 U do know that uppercase characters proceed lowercase characters? I think a sample of the sort of what you are seeing and what you expected should be included Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998172 Share on other sites More sharing options...
Matt79 Posted January 19, 2010 Author Share Posted January 19, 2010 It is included in my previous posts. I tried strtoupper() and it did not change anything. I want it to be in alphabetical order on the webpage. You can see how it is coming out here: http://snyderit.info/test/acc.php Please note the DES near the bottom of the page. The script I am using is here: <?php $raw_data=" customer premises equipment (CPE)& Service provider (SP)& virtual circuits (VC)& Point to Point Tunneling Protocol (PPTP)& Layer 2 Tunneling Protocol (L2TP)& IP security (IPsec)&"; $split=explode("&",$raw_data); sort($split); $num=0; $count=count($split); while($num < $count) { echo $split[$num].'<br/>'; $num++; } ?> I did remove some of the acronyms before I posted it on the form since there are 188 of them. But the number should not matter. Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998207 Share on other sites More sharing options...
laffin Posted January 19, 2010 Share Posted January 19, 2010 I still don't see the problem, all values are returned by their ascii sorting order, Upcase first, lcase second Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998343 Share on other sites More sharing options...
MadTechie Posted January 20, 2010 Share Posted January 20, 2010 My 2cents $raw_data="a customer premises equipment (CPE) z Service provider (SP) y virtual circuits (VC) A Point to Point Tunneling Protocol (PPTP) B Layer 2 Tunneling Protocol (L2TP) 6 IP security (IPsec)"; $split=explode("\n",$raw_data); natcasesort($split); foreach($split as $str){ echo "$str<br/>"; } 6 IP security (IPsec) a customer premises equipment (CPE) A Point to Point Tunneling Protocol (PPTP) B Layer 2 Tunneling Protocol (L2TP) y virtual circuits (VC) z Service provider (SP) Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998350 Share on other sites More sharing options...
salathe Posted January 20, 2010 Share Posted January 20, 2010 It almost appears random. Almost random, except for the extraneous space characters attached to the 'malfunctioning' values. Get rid of that whitespace and I'd hazard a guess that the problem will go away. [ot]Why are you splitting on 1234567890 rather than something more usual like... a line break?[/ot] Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998359 Share on other sites More sharing options...
Matt79 Posted January 20, 2010 Author Share Posted January 20, 2010 Thanks MadTechie. I used the code you created and it worked, although I do not quite understand the differences. And I do not know why the a, z, y, A etc, are in your code. I am assuming it is to explain something but do not get it. a customer premises equipment (CPE) z Service provider (SP) y virtual circuits (VC) A Point to Point Tunneling Protocol (PPTP) B Layer 2 Tunneling Protocol (L2TP) 6 IP security My final script that works thanks to MadTechie: <?php $raw_data=" customer premises equipment (CPE) Service provider (SP) virtual circuits (VC) Point to Point Tunneling Protocol (PPTP) Layer 2 Tunneling Protocol (L2TP) IP security (IPsec) Provider Core (P) "; $raw_data=strtoupper($raw_data); $split=explode("\n",$raw_data); natcasesort($split); foreach($split as $str){ echo "$str<br/>"; } ?> Thanks guys for all the help Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998423 Share on other sites More sharing options...
MadTechie Posted January 20, 2010 Share Posted January 20, 2010 as a note you don't need to convert it to upper case if your using natcasesort Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998608 Share on other sites More sharing options...
Matt79 Posted January 20, 2010 Author Share Posted January 20, 2010 ok thanks for the info. I used upper case to make the page layout look better since I did not fallow the correct English writing rules when I created the page I mixed the lower and upper case. Quote Link to comment https://forums.phpfreaks.com/topic/188992-sortdata/#findComment-998738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.