How to use PHP4 for natural sorting _ PHP Tutorial

Source: Internet
Author: User
How to use PHP4 for natural sorting. For these strings: inf1inf3inf12, if we only use the strcmp () function for comparison, the order is as follows: inf1inf12inf3, which is obviously not the result we want. generally, for these strings:

Inf1
Inf3
Inf12

If we use the strcmp () function for comparison, the order is as follows:

Inf1
Inf12
Inf3

However, this is obviously not the expected result. generally, we use a value of 0 before the number, such as inf01 inf03 inf12. Can we sort out the real order we want without adding this 0?

The following two functions are provided in PHP4 to achieve this:

Int strnatcasecmp (string str1, string str2)
Int strnatcmp (string str1, string str2)

Their supported versions are: PHP4> = 4.0RC2

Description:

Sort str1 str2 by nature order.
Return value <0 indicates that str1 is less than str2,
Return value = 0 indicates that str1 is equal to str2,
If the returned value is greater than 0, str1 is greater than str2.

Example:

$ Str = array ("a", "a0", "a3", "a2", "a10", "a18", "a24 ");

$ K = count ($ str );
Echo "total =". $ k ."
";

Echo"
Sort by strnatcmp
";
For ($ I = 1; $ I <$ k; $ I ++ ){
If (strnatcmp ($ str [$ i-1], $ str [$ I])> 0 ){
$ Tmp = $ str [$ i-1];
$ Str [$ i-1] = $ str [$ I];
$ Str [$ I] = $ tmp;
}
}

Print_r ($ str );
Echo"
Sort by strcmp
";

For ($ I = 1; $ I <$ k; $ I ++ ){
If (strcmp ($ str [$ i-1], $ str [$ I])> 0 ){
$ Tmp = $ str [$ i-1];
$ Str [$ i-1] = $ str [$ I];
$ Str [$ I] = $ tmp;
}
}

Print_r ($ str );

Echo"
Sort by sort
";
Sort ($ str );
Print_r ($ str );
?>

The output is as follows:


Total = 7


Sort by strnatcmp
Array ([0] => a [1] => a0 [2] => a2 [3] => a3 [4] => a10 [5] => a18 [6] => a24)
Sort by strcmp
Array ([0] => a [1] => a0 [2] => a2 [3] => a10 [4] => a18 [5] => a24 [6] => a3)
Sort by sort
Array ([0] => a [1] => a0 [2] => a10 [3] => a18 [4] => a2 [5] => a24 [6] => a3)


Do you understand?

In fact, this function also supports more complex functions. For example:

A <a0 <a1 <a1a <a1b <a2 <a10 <a20

X2-g8 <x2-y7 <x2-y08 <x8-y8

1.001 <1.002 <1.010 <1.02 <1.1 <1.3

Try it. it's really useful.

Inf1 inf3 inf12 if we only use the strcmp () function for comparison, the order obtained is as follows: inf1 inf12 inf3, which is obviously not the expected result...

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.