Jump to content

How to sort Hidden -> A-Z in Top and Show -> Z-A in Bottom


Recommended Posts

<?php

$raw[] = array("hidden" => true, "label" => "Lorem");
$raw[] = array("hidden" => true, "label" => "Ipsum");
$raw[] = array("hidden" => false, "label" => "Dolor");
$raw[] = array("hidden" => false, "label" => "Sit");
$raw[] = array("hidden" => true, "label" => "Amet");
$raw[] = array("hidden" => true, "label" => "Consectetur");
$raw[] = array("hidden" => false, "label" => "Adipiscing");
$raw[] = array("hidden" => false, "label" => "Elit");
$raw[] = array("hidden" => true, "label" => "Sed");

print_r($raw);


?>

Here is data array.. 

Help please. :)

2 hours ago, Ramcult said:

how to use it ? can u explain with code ?

Seriously?  Did you even look at the Manual Page that requinix pointed you to? 

Example #2, down in the comments, gives a worked example of how to define a function that compares two elements of an array, and then invoke usort against an input array and that named function.  How to use it, explained with code.  As requested. 

Regards,
   Phill  W. 

If you want to sort by "hidden" you do not need usort(). A simple sort() or rsort() will suffice.

By default, arrays are sorted on the value of the first element.

$raw[] = array("hidden" => true, "label" => "Lorem");
$raw[] = array("hidden" => true, "label" => "Ipsum");
$raw[] = array("hidden" => false, "label" => "Dolor");
$raw[] = array("hidden" => false, "label" => "Sit");
$raw[] = array("hidden" => true, "label" => "Amet");
$raw[] = array("hidden" => true, "label" => "Consectetur");
$raw[] = array("hidden" => false, "label" => "Adipiscing");
$raw[] = array("hidden" => false, "label" => "Elit");
$raw[] = array("hidden" => true, "label" => "Sed");

sort($raw);
showResults($raw);
rsort($raw);
showResults($raw);

function showResults($array)
{
    echo '<pre>';
    printf("| %6s | %-15s |<br><br>", 'Hidden', 'Label');
    foreach ($array as $arr) {
        printf("| %6s | %-15s |<br>", $arr['hidden'], $arr['label']);
    }
    echo "<pre><br>";
}

Results

sort()                                     rsort()

| Hidden | Label           |               | Hidden | Label           |

|        | Adipiscing      |               |      1 | Sed             |
|        | Dolor           |               |      1 | Lorem           |
|        | Elit            |               |      1 | Ipsum           |
|        | Sit             |               |      1 | Consectetur     |
|      1 | Amet            |               |      1 | Amet            |
|      1 | Consectetur     |               |        | Sit             |
|      1 | Ipsum           |               |        | Elit            |
|      1 | Lorem           |               |        | Dolor           |
|      1 | Sed             |               |        | Adipiscing      |

 

  • Great Answer 1
7 hours ago, Barand said:

If you want to sort by "hidden" you do not need usort(). A simple sort() or rsort() will suffice.

If the arrays are crafted in a very specific way, ie. with the fields set in the order that they should be sorted on. A custom sort is a little (still a one-liner) more complicated but won't suddenly break if the arrays are built differently.

13 hours ago, Barand said:
sort($raw);
showResults($raw);
rsort($raw);
showResults($raw);

function showResults($array)
{
    echo '<pre>';
    printf("| %6s | %-15s |<br><br>", 'Hidden', 'Label');
    foreach ($array as $arr) {
        printf("| %6s | %-15s |<br>", $arr['hidden'], $arr['label']);
    }
    echo "<pre><br>";
}

Sorry, im junior in PHP. 

would you like to explain with code for the condition, this array data have a Hidden:true(1,null) and Label: String. 

i need to regroup the data Hidden and Label first.

ex logic result and the output:

1. Group Hidden and Show
2. $raw have (a,b)
3. a = Hidden -> A-Z into top
    b = Show -> Z-A into top


Sort hidden into top with A-Z (ASC)
Sort Show into bottom with Z-A (DESC) 
1 = Hidden  | ASC
   = Show    | DESC

1 Amet
1 Consectetur
1 Ipsum
1 Lorem
1 Sed

  Adipiscing
  Dolor
  Elit
  Sit 

I don't know how to explain it with code.
Can you help me to explain this with code?
 

7 hours ago, Ramcult said:

Sort hidden into top with A-Z (ASC)
Sort Show into bottom with Z-A (DESC)

That would give this, (not what your example above shows)

|      1 | Amet           \ 
|      1 | Consectetur     |
|      1 | Ipsum           |   HIDDEN, A-Z
|      1 | Lorem           |
|      1 | Sed            /

|        | Sit            \ 
|        | Elit            |   SHOW,   Z-A
|        | Dolor           |
|        | Adipiscing     / 

Is that what you want?

If it is then this is the function...

usort($raw, function ($a, $b) {
    $x = $b['hidden'] <=> $a['hidden'];           // sort hidden to top, show to bottom (ie hidden DESC)
    if ($x == 0) {                                // do elements have same hidden value?
        return $a['hidden'] ? $a['label'] <=> $b['label'] :  $b['label'] <=> $a['label'];    // sort label ASC if hidden, label DESC if show
    }
    else return $x;
});

If it isn't, adapt the function to your requirements.

  • Thanks 1

how to separate column in data array?

For example, the step to separate the data in the hidden column becomes
Hidden = True
Hidden = False?

ex: Hidden = Lorem, Dolor, Sed.
Show = Ipsum, Sit, Amet.


and then, initialize all $raw data to (a and b)

ex:
A = Hidden
B = Show

after that, retrieve data from (a and b)

then (a and b) are sorted based on the data (a and b)

ex:
A = Dolor, Lorem, Sed
B = Sit, Ipsum, Amet

If you haven't written a custom sort function before, I suggest you start with something simple until you understand how they work. EG

$array = [2, 1, 4, 3, 6, 5, 8, 7];

Then write functions to sort the above array

  1. ascending order
  2. descending order
  3. even numbers first followed by the odd numbers

NOTE:

A custom sort function has two arguments, $a and $b. usort() will place each pair of array items into $a and $b and call your function

Your function should determine if $a should sort above or below $b by returning

  • -1 (or a value < 0) if $a should be above $b
  • +1 (or a value > 0) if $a should be below $b
  • 0 if they are considered equal
  • Thanks 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.