sort based on substring

sort based on substring

am 22.01.2008 18:04:31 von brechmos

A bit of a dumb question. I have an array of directories and would
like to sort the array based on the third directory element...

e.g., from...

here/there/200801/everywhere
here/there/200803/everywhere2
here/there/200802/everywhere3

to...

here/there/200801/everywhere
here/there/200802/everywhere2
here/there/200803/everywhere3

I can think of lots of unelegant ways, is there an elegant one? (few
lines of code?)

Thanks...

Re: sort based on substring

am 22.01.2008 18:05:47 von brechmos

I need to reply to my stupidty already...

assume it is

here1/there/200801/everywhere
here2/there/200803/everywhere2
here3/there/200802/everywhere3

to...

here1/there/200801/everywhere
here3/there/200802/everywhere2
here2/there/200803/everywhere3

otherwise a simple sort command would work...

On Jan 22, 12:04 pm, brechmos wrote:
> A bit of a dumb question. I have an array of directories and would
> like to sort the array based on the third directory element...
>
> e.g., from...
>
> here/there/200801/everywhere
> here/there/200803/everywhere2
> here/there/200802/everywhere3
>
> to...
>
> here/there/200801/everywhere
> here/there/200802/everywhere2
> here/there/200803/everywhere3
>
> I can think of lots of unelegant ways, is there an elegant one? (few
> lines of code?)
>
> Thanks...

Re: sort based on substring

am 22.01.2008 18:28:33 von luiheidsgoeroe

On Tue, 22 Jan 2008 18:04:31 +0100, brechmos wrote:=


> A bit of a dumb question. I have an array of directories and would
> like to sort the array based on the third directory element...
>
> e.g., from...
>
> here/there/200801/everywhere
> here/there/200803/everywhere2
> here/there/200802/everywhere3
>
> to...
>
> here/there/200801/everywhere
> here/there/200802/everywhere2
> here/there/200803/everywhere3
>
> I can think of lots of unelegant ways, is there an elegant one? (few
> lines of code?)

function _mysort($a,$b){
$a_array =3D explode('/',$a);
$asorter =3D isset($a_array[2]) ? $a_array[2] : '';
$b_array =3D explode('/',$b);
$bsorter =3D isset($b_array[2]) ? $b_array[2] : '';
return strcmp($asorter,$bsorter);
}
usort($array,'_mysort');

-- =

Rik Wasmus

Re: sort based on substring

am 22.01.2008 20:17:24 von brechmos

I knew there must be a good answer. Thanks.


>
> function _mysort($a,$b){
> $a_array = explode('/',$a);
> $asorter = isset($a_array[2]) ? $a_array[2] : '';
> $b_array = explode('/',$b);
> $bsorter = isset($b_array[2]) ? $b_array[2] : '';
> return strcmp($asorter,$bsorter);}
>
> usort($array,'_mysort');
>
> --
> Rik Wasmus

Re: sort based on substring

am 23.01.2008 02:51:50 von Csaba Gabor

On Jan 22, 6:05 pm, brechmos wrote:
> I need to reply to my stupidty already...
>
> assume it is
>
> here1/there/200801/everywhere
> here2/there/200803/everywhere2
> here3/there/200802/everywhere3
>
> to...
>
> here1/there/200801/everywhere
> here3/there/200802/everywhere2
> here2/there/200803/everywhere3
>
> otherwise a simple sort command would work...
>
> On Jan 22, 12:04 pm, brechmos wrote:
>
> > A bit of a dumb question. I have an array of directories and would
> > like to sort the array based on the third directory element...
>
> > e.g., from...
>
> > here/there/200801/everywhere
> > here/there/200803/everywhere2
> > here/there/200802/everywhere3
>
> > to...
>
> > here/there/200801/everywhere
> > here/there/200802/everywhere2
> > here/there/200803/everywhere3
>
> > I can think of lots of unelegant ways, is there an elegant one? (few
> > lines of code?)
>
> > Thanks...

Here is an alternate way, provided each
element is unique. Assume the array to
be sorted is in $array (4 lines, watch
for wrapping):


for ($aSort=array(),$i=0;$i $aSort[$val]=preg_replace("'^([^/]*/){2}'","",$val=$array[$i ]);
asort($aSort);
$array = array_keys ($aSort);


If there are duplicate values or the keys are not
indexed from 0 to sizeof($array)-1 then the for loop
should be a foreach and the sorting section becomes
a bit more involved:


$aSorted = array();
foreach ($array as $key=>$val)
$aSorted[$key]=preg_replace("'^([^/]*/){2}'","",$val);
asort($aSorted);
$aSorted = array_keys($aSorted);
foreach ($aSorted as &$key) $key=$array[$key];

and $aSorted now has the sorted $array
(perhaps also compare my example
at http://php.net/asort

If it is known that all the keys in
$array are non numeric,
then the last two lines could be combined to:
$aSorted = array_merge(array_flip (array_keys($aSorted)), $array);

Csaba Gabor from Vienna