php - 为什么 SORT_REGULAR 在 PHP 中产生不一致的结果?

标签 php arrays sorting

我正在研究一个使 PHP 中的数组排序更容易的类,并且我一直在使用 SORT_ 常量,但是行为或 SORT_REGULAR(默认排序类型)似乎有所不同取决于您在数组中添加项目的顺序。此外,我无法找出为什么会出现这种情况的模式。

数组项:

$a = '0.3';
$b = '.5';
$c = '4';
$d = 'F';
$e = 'z';
$f = 4;

场景 1:

sort(array($d, $e, $a, $f, $b, $c));

// Produces...
array(6) {
  [0]=>
  string(3) "0.3"
  [1]=>
  string(2) ".5"
  [2]=>
  string(1) "4"
  [3]=>
  string(1) "F"
  [4]=>
  string(1) "z"
  [5]=>
  int(4)
}

场景 2:

sort(array($d, $e, $b, $f, $c, $a));

// Produces...
array(6) {
  [0]=>
  string(3) "0.3"
  [1]=>
  string(2) ".5"
  [2]=>
  string(1) "F"
  [3]=>
  string(1) "z"
  [4]=>
  int(4)
  [5]=>
  string(1) "4"
}

有什么想法吗?

最佳答案

http://php.net/sort

Warning

Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.

您应该使用 SORT_* 常量之一。

这里有几点评论:

关于php - 为什么 SORT_REGULAR 在 PHP 中产生不一致的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20792028/

相关文章:

python - PySpark takeOrdered 多个字段(升序和降序)

Java 对象 Random 总是返回错误 : "Random.nextInt(int) line: not available"

java - 如何从java数组中获取非随机样本

ios - Swift - 函数在附加字典中的键值后仅返回 nil?

python - 这种排序有名称吗?它对哪种数据排序有效?

php - HTML : Enable Multiple Submission without refreshing

php - 重定向 header

php - 防止自引用表中的主键和外键重复

php - 如何创建与 yii2-user 的关系?

java - 使用 Java 8,创建排序和分组字符串列表的最简洁方法是什么