Natural sorting of strings

Source: Internet
Author: User
Tags natural string
Natural sorting of strings 1. $ a = 'x2-g8 '; $ B = 'x2-y7'; echostrnatcasecmp ($ a, $ B ); // output-1 ------------------------------------------------------------- natural sorting of strings
1,
$ A = 'x2-g8 ';
$ B = 'x2-y7 ';
Echo strnatcasecmp ($ a, $ B); // output-1
---------------------------------------------
2,
$ A = 'a10a ';
$ B = 'a010 ';
Echo strnatcasecmp ($ a, $ B); // output 1
---------------------------------------------
3,
$ A = '10a ';
$ B = '10. 0a ';
Echo strnatcasecmp ($ a, $ B); // output 1
---------------------------------------------

Problem:
1. what is written in the manual is:
Strings can contain several number parts: x2-g8 <x2-y7 <x2-y08 <x8-y8
In which case numeric fields are separated by nonnumeric characters. Leading spaces are ignored.
Does this mean that the leading position can be ignored? Isn't $ a and $ B 28 or 27?
2. aren't $ a and $ B both 10? Why is it considered different?
3. I still think $ a and $ B are both 10 ~

I read the "natural string sorting" rules in the manual, but I am not very familiar with them. can you tell me some rules for natural string sorting?
In addition, is the natural sorting of strings and the natural sorting (natcasesort () of arrays a rule?

Thank you!

------ Solution --------------------
Never used! You can sort by array, that is, natural sorting!

It is generally sorted by letters!

Can be converted to gbk and then sorted. After converting to the corresponding language code, you can directly compare the size.
Uppercase and lowercase letters are different.


------ Solution --------------------

#1. if the first character string is 0, it will be ignored. Therefore, the two examples below show different results.

Echo strnatcasecmp ('7', '08'); //-1
Echo strnatcasecmp ('y7', 'y08'); // 1

#2. spaces in the string will be ignored

#3. if it is a non-numeric pair or a non-numeric pair, the ascii code prevails.
A) any digit string starting with 0 is regarded as a small one. for example
Echo strnatcasecmp ('y721', 'y07203 '); // 1, because y = y, 7> 0
B). if none of them are 0, the smaller party is a smaller party, for example
Echo strnatcasecmp ('y723', 'y70212 '); // 1, because y = y, 70212> 723

You can read the source code, which is easy to understand.
------ Solution --------------------
Echo strnatcasecmp ('7', '08'); //-1
Echo strnatcasecmp ('y7', 'y08'); // 1

-11

$ A = 'x2-g8 ';
$ B = 'x2-y7 ';
Echo strnatcasecmp ($ a, $ B); // output-1
---------------------------------------------
2,
$ A = 'a10a ';
$ B = 'a010 ';
Echo strnatcasecmp ($ a, $ B); // output 1
---------------------------------------------
3,
$ A = '10a ';
$ B = '10. 0a ';
Echo strnatcasecmp ($ a, $ B); // output 1

-111


Is there anything wrong?
String comparison is always compared by byte until the corresponding bit, compare the corresponding two characters

If the description is not in line with this explanation, please note the fact that foreigners are not God
If you only understand the difference, please beat your English teacher's ass.

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.