Jump to content

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\" /]
Link to comment
https://forums.phpfreaks.com/topic/10441-sorting-a-file/
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.
Link to comment
https://forums.phpfreaks.com/topic/10441-sorting-a-file/#findComment-39818
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.