PHP, sort, sort_flags
Asked Answered
V

1

6

I am studying sort_flags at this page on PHP Manual.

And I don't understand what difference each of these flags represents.

There are only 6 flags, can someone please help me to understand the difference between them. Maybe with some examples. I would be very thankful.

Veronicaveronika answered 23/6, 2012 at 9:50 Comment(1)
Blanktext, don't forget to tick (ideally) or +1 answers you find useful.Ritualism
A
10

Array used for testing:

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

Note that 3.14 & 2.72 are strings.

Using SORT_REGULAR flag (compare items normally):

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

Using SORT_NUMERIC flag (compare items numerically, so 3.14 is sorted as number not a string):

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

Using SORT_STRING flag (SORT_LOCALE_STRING works similary, but depends on current locale, all values are treated as strings):

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

Using SORT_NATURAL flag (note order of img* strings, it is natural):

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

SORT_FLAG_CASE can be combined with SORT_STRING or SORT_NATURAL to do case-insensitive sort e.g.:

// works like SORT_NATURAL but is case-insensitive
sort($toSort, SORT_NATURAL | SORT_FLAG_CASE);
Anoxia answered 23/6, 2012 at 10:43 Comment(14)
Wow thank you des i was looking at the link but it didnt tell me much, but now..., thank you i am reading.Veronicaveronika
Later I thought it wouldn't be enough, so I decided to answer. This is accually good question. I haven't tested those for uppercase strings, but this should be sufficient.Anoxia
Why when i am trying use SORT_NATURAL the output is not the same is yours this is my code:pastebin.com/XtiDwg61 , and thank you.Veronicaveronika
The code is the same code for sure, but ideone is using PHP (php 5.2.11) and i am using wamp with (php 5.4.3) why do you think there is a difference between those two results?Veronicaveronika
I see, then it shouldn't work at all, because SORT_NATURAL was added in PHP 5.4. I've updated my answer with correct SORT_NATURAL results. Thanks for noticing!Anoxia
The SORT_REGULAR is still not really clear to me. How do they consider one value smaller than another?Pandurate
@Pandurate sort() : "SORT_REGULAR - compare items normally (don't change types)"Anoxia
@Pandurate sort() : "WARNING Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results."Anoxia
@Pandurate take a look at Why does SORT_REGULAR produce inconsistent results in PHP?Anoxia
@walkhard I saw the explanation from the manual, and I think it is stupid :) I mean "normally", what does this even mean? Maybe it means that two numbers are compared numerically, two strings are compared lexicographically, and string vs number we flip a coin? Or maybe it follows the behavior of the < and > operators?Pandurate
3v4l.org/KCZo7 assert('12' > '2' && '2' > '13 ' && '13 ' > '12'); circular, yeah! So if both of the two values look like numbers, numeric comparison happens. If less than both of them do, string comparison applies. How really bad :)Pandurate
Ok apparently it is like this: If two strings both look like a number, or one of the two values is a number, numeric comparison applies. Otherwise, string comparison applies.Pandurate
Possibly, thought I guess it's unreliable.Anoxia
This helped fix my Laravel 5.2 Collection sorting. Laravel documentation doesn't show this as an option (laravel.com/docs/5.2/collections#method-sortby), although the sortBy function in the Collection.php file does show it uses SORT_REGULAR as the default second parameter (which made sense in hindsight). I ended up going with: Item::where($itemQueryArray)->with($modelName)->get()->sortBy($sortText, SORT_STRING | SORT_FLAG_CASE);Seibert

© 2022 - 2024 — McMap. All rights reserved.