Jump to content

Archived

This topic is now archived and is closed to further replies.

raila

Sorting a file

Recommended Posts

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øm

I Need it to sort these lines by the number infornt of the name, but it needs to
write the name.

eksample:
lines read:
<9>Ham-Kam
<14>Rosenborg
<21>Brann

The file is sorted here.
And then it produses this result:

Lines typed:
Ham-Kam
Rosenborg
Brann


What i want to bee written is the "opposit":
Brann
Rosenborg
Ham-Kam


I 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\" /]

Share this post


Link to post
Share on other sites
You could run it through rsort() afterwards to sort the array in reverse.

Share this post


Link to post
Share on other sites
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\" /]

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.