Re: Basic Dual Query Combine into one question

From: Date: Fri, 30 Jan 2015 02:02:44 +0000
Subject: Re: Basic Dual Query Combine into one question
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-324327@lists.php.net to get a copy of this message
I can't thanks you enough for your excellent help - but I'll try... THANK YOU - THANK YOU - THANK YOU ! On Jan 29, 2015, at 5:57 PM, Christoph Becker <cmbecker69@gmx.de> wrote: > dealTek wrote: > >> Q: I think when I tried to sort on numeric field is came back as text >> type sort... >> >> Is there a way to designate a numeric sort on certain fields? > > Yes, of course. An example is given on the manual page[1]. However, I > consider that somewhat clumsy. Instead you could do: > > function cmp($a, $b) > { > return $a - $b; > } > > I suggest to mentally walk through some examples (e.g. cmp(3, 4); cmp(4, > 3); cmp(3,3)) to better understand what's happening. > >> Q: Is it also possible to sort a more than 1 field like sort on >> Contact_Last & also State field at same time? > > Yes, for instance: > > function cmp($a, $b) > { > $result = strcmp( > $a->getField('Contact_Last'), > $b->getField('Contact_Last') > ); > // indifferent, i.e. last names are identical, so sort by state > if (!$result) { > $result = strcmp( > $a->getField('State'), > $b->getField('State') > ); > } > return $result; > } > > Probably you want to exchange the conditions (i.e. sort by state first, > then sort by last name), but that depends on your use-case. > > Note, that strcmp() is case sensitive, and that there is strcasecmp() > which is case insensitive. Also note, that both depend on the current > locale AFAIK, and that it might be necessary to use the Collator class[2]. > > Sorting can get very tricky... :( > > [1] <http://php.net/manual/en/function.usort.php> > [2] <http://php.net/manual/en/class.collator.php> > > -- > Christoph M. Becker -- Thanks, Dave - DealTek dealtek@gmail.com [db-15]

« previous php.general (#324327) next »