Understanding php array sort order

I have this array

$array = array(2, 1, "img1", "img2", "img10", 1.5, "3.14", "2.72");

after applying the function, sortit becomes

sort($array);

Array
(
    [0] => 2.72
    [1] => 3.14
    [2] => img1
    [3] => img10
    [4] => img2
    [5] => 1
    [6] => 1.5
    [7] => 2
)

I did not understand how sorting is done.

Can someone explain how the output array is reached?

EDIT:

The question here is not what to use SORT FLAG, but the question is how the above sorting is done. Any advice on using another SORT FLAGhere is useless.

+5
source share
3 answers

Using the link information for Coanda as a starting point, it is clear that PHP uses type manipulation when comparing objects of different types. The problem is understanding what rushes into that during the comparison, and I still could not find the complete list, but in general:

int

(int) string <,>,=, (int) int 

0, ints . (). , .

PHP quicksort , , array[n/2], n - . , :

$pivot = $array[n/2] //n is the number of elements, this sets $pivot='img2'
//compare each element in the list (i am going to this by hand for demonstration)

(int) 'img2' < 2 //int to int comparison;'img2' resolves to 0 and 0 < 2
(int) 'img2' < 1 //int to int comparison;'img2' resolves to 0 and 0 < 1
      'img2' > 'img1' // string to string comparison; strcmp is +256 
      'img2' > 'img 10' //string to string comparison; strcmp is +256 
(float) 'img2' < 1.5 //float to float comparison;'img2' resolves to 0 and 0<1.5 
      'img2' > '3.14' //string to string comparison; strcmp is +54
      'img2' > '2.72' //string to string comparison; strcmp is +55

( ).

 $greater = array('img1', 'img10', '3.14', '2.72);
 $less = array(2, 1, 1.5);

, 2 , . $greater , , .

sort($greater);
var_dump($greater);

array(4) {
  [0]=>
  string(5) "2.72"
  [1]=>
  string(4) "3.14"
  [2]=>
  string(4) "img1"
  [3]=>
  string(5) "img10"
}

, , . $lesser

$lesser = array(2, 1, 1.5);
sort($lesser);
var_dump($lesser);

array(3) {
  [0]=>
  int(1)
  [1]=>
  float(1.5)
  [2]=>
  int(2)
}

. , ( "img2" ). .

Array
(
[0] => 2.72
[1] => 3.14
[2] => img1
[3] => img10
[4] => img2
[5] => 1
[6] => 1.5
[7] => 2
)

, , $arr[3] .

$arr = array("img2", 1, "img1", 2, "img10", 1.5, "3.14", "2.72");
sort($arr);
var_dump($arr)

, int, float float.

array(8) {
[0]=>
string(4) "img1"
[1]=>
string(5) "img10"
[2]=>
string(4) "img2"
[3]=>
int(1)
[4]=>
float(1.5)
[5]=>
int(2)
[6]=>
string(4) "2.72"
[7]=>
string(4) "3.14"
}
+3

( ), : .

( ), , .

array(8)
    0 => '2.72'    [alphabetical]
    1 => '3.14'    [alphabetical]
    2 => 'img1'    [alphabetical]
    3 => 'img10'   [alphabetical]
    4 => 'img2'    [alphabetical]
    5 => 1         [numeric]
    6 => 1.5       [numeric]
    7 => 2         [numeric]

, , (a, b, c...) (1,2,3..), .

, , , . .


, (:

.

+6

: https://bugs.php.net/bug.php?id=21728

:

<?php
$arr1 = array("a","b","c","d","4",5,4,"true","TRUE",true);
sort($arr1);
var_dump($arr1);
?>

The output is :
array(10) {
  [0]=>
  bool(true)
  [1]=>
  int(4)
  [2]=>
  string(1) "4"
  [3]=>
  string(4) "TRUE"
  [4]=>
  string(1) "a"
  [5]=>
  string(1) "b"
  [6]=>
  string(1) "c"
  [7]=>
  string(1) "d"
  [8]=>
  string(4) "true"
  [9]=>
  int(5)
}

- (int) 5 - . , "4" , (int) 5, "4" "" "" 5. 2 , - . . . 5 "5", "5" "4" .

+1

All Articles