raila Posted May 25, 2006 Share Posted May 25, 2006 I need to sort a file, but get the result in "reverse".Anyone knows how to change this ?The file that is to bee sorted contains the following information:<6>Tromsø<7>Start<9>Fredrikstad<9>Ham-Kam<9>Vålerenga<10>Viking<11>Lyn<11>Molde<11>Sandefjord<12>Stabæk<14>Odd-Grenland<14>Rosenborg<21>Brann<21>LillestrømI Need it to sort these lines by the number infornt of the name, but it needs towrite the name.eksample:lines read:<9>Ham-Kam<14>Rosenborg<21>BrannThe file is sorted here.And then it produses this result:Lines typed:Ham-KamRosenborgBrannWhat i want to bee written is the "opposit":BrannRosenborgHam-KamI use the following lines to do this:$alt = file(the file to bee sortet, Needs to use natsort since that is the only sorting that works for this file)natsort($alt);is there a command or anything to make natsort work in "reverse"?Thanks for all help :-) [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10441-sorting-a-file/ Share on other sites More sharing options...
zq29 Posted May 25, 2006 Share Posted May 25, 2006 You could run it through rsort() afterwards to sort the array in reverse. Quote Link to comment https://forums.phpfreaks.com/topic/10441-sorting-a-file/#findComment-38950 Share on other sites More sharing options...
raila Posted May 26, 2006 Author Share Posted May 26, 2006 Thanks. That worked just fine.Sorry for sutch a stupid question, but i just coulden't think of it :-)Raila [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10441-sorting-a-file/#findComment-39284 Share on other sites More sharing options...
Barand Posted May 29, 2006 Share Posted May 29, 2006 [code]$alt = array('<6>Tromsø','<7>Start','<9>Fredrikstad','<9>Ham-Kam','<9>Vålerenga','<10>Viking','<11>Lyn','<11>Molde','<11>Sandefjord','<12>Stabæk','<14>Odd-Grenland','<14>Rosenborg','<21>Brann','<21>Lillestrøm');[/code]if you use rsort() after the natsort() [code]natsort($alt);rsort($alt);[/code]then you scramble the results into[code]Array( [0] => <9>Vålerenga [1] => <9>Ham-Kam [2] => <9>Fredrikstad [3] => <7>Start [4] => <6>Tromsø [5] => <21>Lillestrøm [6] => <21>Brann [7] => <14>Rosenborg [8] => <14>Odd-Grenland [9] => <12>Stabæk [10] => <11>Sandefjord [11] => <11>Molde [12] => <11>Lyn [13] => <10>Viking)[/code]but if you use array_reverse()[code]natsort($alt);$alt = array_reverse($alt);[/code]you get[code]Array( [0] => <21>Lillestrøm [1] => <21>Brann [2] => <14>Rosenborg [3] => <14>Odd-Grenland [4] => <12>Stabæk [5] => <11>Sandefjord [6] => <11>Molde [7] => <11>Lyn [8] => <10>Viking [9] => <9>Vålerenga [10] => <9>Ham-Kam [11] => <9>Fredrikstad [12] => <7>Start [13] => <6>Tromsø)[/code]which I suspect is nearer your desired outcome. Quote Link to comment https://forums.phpfreaks.com/topic/10441-sorting-a-file/#findComment-39818 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.