问题描述:

I was attempting to sort some strings in an array alphabetically. The strings were file names I grabbed from a directory using glob. At first, I thought glob was incorrectly sorting the file names. I tried sort, and the same thing happened: 1874S-O.jpg came before 1874S.jpg

Finally, I compared them with

var_dump("11-09-2016/1874S/1874S-O.jpg" > "11-09-2016/1874S/1874S.jpg");

which returned bool(false). Any ideas why the longer string would not be greater than the shorter string and would also not come after the shorter string alphabetically?

网友答案:

Because . is greater than -.

Those are the first non-equal characters in your strings.

网友答案:

PHP is sorting this by ASCII decimal value. The sorting compares until it finds unequal values.

. has a value of 46

- has a value of 45

For kicks, you can check out this demo, where % is also less than .

相关阅读:
Top