Several Methods for sorting PHP-PHP source code

Source: Internet
Author: User
For sorting common data, we can directly use the system's built-in array sorting function. If it is a relational array sorting, do you know how to operate? Here are a few examples for you. Let's take a look. For sorting common data, we can directly use the system's built-in array sorting function. If it is a relational array sorting, do you know how to operate? Here are a few examples for you. Let's take a look.

Script ec (2); script

Parameter description: $ data destination array $ key sorting field, $ sort sorting rule

The Code is as follows:

Function getSortArray ($ data, $ key, $ sort = 'asc ')
{
If (! Is_array ($ data ))
{
Return false;
}

$ Len = count ($ data );

For ($ k = 1; $ k <$ len; $ k ++)
{
For ($ j = $ len-1, $ I = 0; $ I <$ len-$ k; $ I ++, $ j --)
{
If (strtoupper ($ sort) = 'desc ')
{
$ Flag = $ data [$ j] [$ key]> $ data [$ J-1] [$ key];
}
Else
{
$ Flag = $ data [$ j] [$ key] <$ data [$ J-1] [$ key];
}

If ($ flag)
{
$ Tmp = $ data [$ j];
$ Data [$ j] = $ data [$ J-1];
$ Data [$ J-1] = $ tmp;
}
}
}
Return $ data;
}

Usage:

$ A = array (
Array ('name' => 'stone ', 'age' => 17, 'sex' => 'male '),
Array ('name' => 'sunny ', 'age' => 28 ),
Array ('name' => 'grass ', 'age' => 16, 'sex' => 'female '),
Array ('name' => 'sea', 'age' => 15, 'birthday' => '2017-08-16 ')
);
Print_r (getSortArray ($ a, 'age', 'desc '));

Result:
Array

(

[0] => Array

(

[Name] => sunny

[Age] => 28

)

[1] => Array

(

[Name] => stone

[Age] => 17

[Sex] => male

)

[2] => Array

(

[Name] => grass

[Age] => 16

[Sex] => female

)

[3] => Array

(

[Name] => sea

[Age] => 15

[Birthday] => 1987-08-16

)

)

Do you understand this? Let's look at two examples of array sorting.

To sort the array's age fields, php's simple sort function obviously cannot meet the requirements. Therefore, you need to write a Quick sort code to implement the corresponding requirements.

The Code is as follows:

/**
* Description: gets the location of the central point.
*
* @ Param array $ array
* @ Param int $ left
* @ Param int $ right
* @ Param string $ field
* @ Return int
*/
Function fetcharrayray( & $ array, $ left, $ right, $ field)
{
// Benchmark Definition
$ Stand = $ array [$ left];

// Traverse the Array
While ($ left <$ right ){
While ($ left <$ right & $ array [$ right] [$ field] >=$ stand [$ field]) {
$ Right --;
}
If ($ left <$ right ){
$ Array [$ left ++] = $ array [$ right];
}

While ($ left <$ right & $ array [$ left] [$ field] <= $ stand [$ field]) {
$ Left ++;
}
If ($ left <$ right ){
$ Array [$ right --] = $ array [$ left];
}
}

// Obtain the central point
$ Array [$ left] = $ stand;

Return $ left;
}

/**
* Description: quick sorting of the main program
*
* @ Param array $ array
* @ Param int $ begin
* @ Param int $ end
* @ Param string $ field
*/
Function quickSort (& $ array, $ begin, $ end, $ field)
{
// Variable definition
$ Activities = null;

If ($ begin <$ end ){
$ Fields = fetcharrayray( $ array, $ begin, $ end, $ field );
QuickSort ($ array, $ begin, $ fields-1, $ field );
QuickSort ($ array, $ struct + 1, $ end, $ field );
}
}
2. Call example:

$ Array = array (
Array (
'Name' => "xiao ",
'Age' => 3
),
Array (
'Name' => 'wang ',
'Age' => 1
),
Array (
'Name' => 'chen ',
'Age' => 2
),
Array (
'Name' => 'zhengyi ',
'Age' => 4
),
Array (
'Name' => 'chai ',
'Age' => 14
)
);

$ Begin = getTime ();
QuickSort ($ array, 0, count ($ array)-1, 'age ');
Print_r ($ array );
$ End = getTime ();
$ Spend = $ end-$ begin;

Echo "time spent:". $ spend. "seconds n ";

Function getTime ()
{
List ($ msec, $ sec) = explode ("", microtime ());
Return (float) $ msec + (float) $ sec;
}

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.