Jump to content


Photo

Sorting a file


  • Please log in to reply
3 replies to this topic

#1 raila

raila
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 25 May 2006 - 03:21 PM

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

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 25 May 2006 - 04:08 PM

You could run it through rsort() afterwards to sort the array in reverse.

#3 raila

raila
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 26 May 2006 - 08:41 PM

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

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 May 2006 - 12:44 AM

$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'
);

if you use rsort() after the natsort()
natsort($alt);
rsort($alt);

then you scramble the results into
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
)

but if you use array_reverse()
natsort($alt);
$alt = array_reverse($alt);


you get
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ø
)

which I suspect is nearer your desired outcome.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users