Re: Basic Dual Query Combine into one question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux