Re: PHP sort from .... best solution?

From: Date: Thu, 05 Jun 2003 00:16:46 +0000
Subject: Re: PHP sort from .... best solution?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-28328@lists.php.net to get a copy of this message
hi the easier way is as Mike Ford did it very elegantly with this structur: > function obj_date_compare($a, $b) { > return strcmp($a->publisertLang, $b->publisertLang); > } > > ... > > if($ingresser = array_merge($i1 ,$i2)) > { > $ingresser = usort($ingresser, 'obj_date_compare'); > listIngresser($ingresser); > } the for-structure from your admin guy is also a possibility, but i think php offers cool sort-functions like this usort-function (which was also new to me - thx@Mike!). so it is not necessary to write your own sorting-algorithm. .ma A. Lyse <allyse@hardware.no> wrote@04.06.2003 13:40 Uhr: > Hi again! > > I talked to the admin guy and he said there is an easyer way to do this (as > usual he doenst have the time to show me) so I try here: > > He gave me a liddle example in c and a light translation to PHP: > > Bubble sort (exampel written in c, but can "easyly" be translated to php): > for (i=0; i<n-1; i++) { > for (j=0; j<n-1-i; j++) > if (a[j+1] < a[j]) { /* compare the two neighbors */ > tmp = a[j]; /* swap a[j] and a[j+1] */ > a[j] = a[j+1]; > a[j+1] = tmp; > } > } > > ************** > > More example of Bubble Sort: > > http://www.cs.ust.hk/faculty/tcpong/cs102/summer96/aids/bubble.html > > The if-sentense must here be written to > if($a[$j+1]->publisertLang < $a[$j]->publisertLang) > where a is $ingresser and $n is the number of elements in $ingresser. > > I > > The if sentese is going in the php file pastet yesterday. > > Not that yeasterdays metod did'nt work, but if somebody has an idea on this > one and can give me a full example based on the info i've pastet yesterday > it would be great! > > Regards, > A. Lyse > >

« previous php.db (#28328) next »