Summary of PHP sorting functions

Source: Internet
Author: User

Sort destroys the sorting of index values in ascending order

Rsort destroys sorting of index values in descending order

 

Asort sort index values in ascending order

Arsort keeps the index sorted in descending order

 

Ksort keeps indexes sorted by key in ascending order)

Krsort keeps the index sorted by key in descending order)

 

Usort

This function uses a user-defined comparison function to sort the values in an array. If the array to be sorted needs to be sorted by an unusual standard, use this function.

A comparison function must return an integer less than, equal to, or greater than zero when the first parameter is considered to be less than, equal to, or greater than the second parameter.

Uksort

Uksort-sort the keys in the array using the User-Defined comparison Function

Cmp_functionThe function should accept two parameters, which will beArrayIn. When the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than zero, equal to zero, or greater than zero.

Natsort

This function implements a sorting algorithm that is the same as the method that people usually sort character strings and maintain the association of original keys/values. This is called "natural sorting ". This algorithm and the general computer string Sorting Algorithm

 

Bool natcasesort (array & $ array)Case Insensitive

This function implements a sorting algorithm that is the same as the method that people usually sort character strings and maintain the association of original keys/values. This is called "natural sorting ".

Return if successfulTRUE, Or return when the failure occurs.FALSE.

For example, to sort two-dimensional arrays, use the built-in functions asort and arsort.

The idea is to keep the key values of $ keyvalue and $ arr the same, and then assign the column $ arr wants to sort to $ keyvalue, and use the built-in function to sort $ keyvalue,

Finally, other columns of $ arr are returned Based on the unchanged key value.

 

Function array_sort ($ arr, $ keys, $ type = 'asc ')
{
Echo 'start sorting... '.' </br> ';
$ Keysvalue = $ new_array = array ();
Foreach ($ arr as $ k => $ v)
{
$ Keysvalue [$ k] = $ v [$ keys];
}
If ($ type = 'asc ')
{

}
Else
{
 
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v)
{
$ New_array [$ k] = $ arr [$ k];
}
Echo 'sorting ends... '.' </br> ';
Return $ new_array;
}

 

 

 

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.