PHP-implemented custom array sort functions and sort class examples _php tips

Source: Internet
Author: User
Tags array sort exception handling index sort php error

This article illustrates the custom array sort functions and sorting classes implemented by PHP. Share to everyone for your reference, specific as follows:

*
* * Two-dimensional array custom sort function
* Uasort ($arr, function_name)
*
**/
$arr = array (
  array (' A ' =>1, ' B ' => ' C '),
  array (' A ' =>4, ' B ' => ' a '),
  array (' A ' =>5, ' B ' => ' G '),
  array (' A ' =>7, ' B ' => ' F '),
  Array (' A ' =>6, ' B ' => ' e ')
);
function Compare_arr ($x, $y) {
  if ($x [' B ']< $y [' B ']) {
    return-1;
  } else if ($x [' B ']> $y [' B ']) {return
    1;
  } else{return
    0;
  }
Uasort ($arr, ' Compare_arr ');
foreach ($arr as $a) {
  echo $a [' a ']. ' => '. $a [' B ']. ' <br/> ';
}

Custom sort classes in the manual:

 class Multisort {var $key; The key in your array//Sort function argument is an array to arrange the index sort type functions run ($myarray, $key _to_sort, $type _of_sort = ") {$
     This->key = $key _to_sort;
     if ($type _of_sort = = ' desc ') uasort ($myarray, Array ($this, ' myreverse_compare '));
     else Uasort ($myarray, Array ($this, ' mycompare '));
   return $myarray;
     }//Positive sequence function Mycompare ($x, $y) {if ($x [$this->key] = = $y [$this->key]) return 0;
     else if ($x [$this->key] < $y [$this->key]) return-1;
   else return 1;
     //Reverse function Myreverse_compare ($x, $y) {if ($x [$this->key] = = $y [$this->key]) return 0;
     else if ($x [$this->key] > $y [$this->key]) return-1;
   else return 1; }
}

More interested in PHP related content readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP string (String) Usage summary", "PHP for XML file Operating skills summary", " PHP error and exception handling method summary, "PHP operation and operator Usage Summary", "PHP basic Grammar Introductory Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database Operation Introduction Course" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.