0

I wanted to sort php array based on CRITICAL , WARNING ,INFO sub string and then CRITICAL , WARNING ,INFO sub array should be sorted again with the time stamp value contains in each line of string in acsending order. Basically at the end I need array to be sorted with CRITICAL 1st with time stamp sorted then WARNING and then INFO so on..

kkr
  • 1
  • 1
  • nothing is clear.... – Naincy Nov 24 '14 at 06:47
  • Please provide your array format – Sreeraj Nov 24 '14 at 06:49
  • $keys= (array_keys($eventinfo["message"])); for ( $i = 0; $i < count($keys); $i++ ){ $key = ($keys[$i]); $perNode= $eventinfo["message"][$key]; $arrTmp=explode("\n",$perNode); $t=count($arrTmp); $tmp = 0; while($t){ $t--; if($arrTmp[$tmp]!=""){ $cumltvArray[] ="". $key." : ".$arrTmp[$tmp]; } $tmp++; } } $result_str = events_print($cumltvArray); $cumltvArray is the one which I am passing to events_print – kkr Nov 24 '14 at 07:08

2 Answers2

1

First, define a function that turns the urgency of a line into a number.

function urgency($line)
{
    if (strpos($line, 'INFO') !== false) {
        return 1;
    } elseif (strpos($line, 'WARNING') !== false) {
        return 2;
    } elseif (strpos($line, 'CRITICAL') !== false) {
        return 3;
    }
    return 0;
}

Then, assuming each element of your array contains a line of the file, you need to apply a decorator to keep the sort stable; see also my earlier answer on the subject:

array_walk($array, function(&$element, $index) {
    $element = array($element, $index); // decorate
});

After applying the decorator, you sort the array; I'm using a stable comparison helper:

function stablecmp($fn)
{
    return function($a, $b) use ($fn) {
        if (($tmp = call_user_func($fn, $a[0], $b[0])) != 0) {
            return $tmp;
        } else {
            return $a[1] - $b[1];
        }
    };
}

usort($array, stablecmp(function($a, $b) {
    return urgency($b) - urgency($a);
}));

Finally, undecorate the array to produce the end result:

array_walk($array, function(&$element) {
    $element = $element[0];
});
Community
  • 1
  • 1
Ja͢ck
  • 161,074
  • 33
  • 239
  • 294
  • Jack, could you explain the syntaxt of the last part of your code? The usort part. Never seen PHP written this way. What does function($a,$b) do? How does it this work? I also dont understand your return statement. Thanks in advance :) – Bolli Nov 24 '14 at 07:00
  • 1
    @Bolli I'm using [anonymous functions](http://php.net/manual/en/functions.anonymous.php). – Ja͢ck Nov 24 '14 at 07:05
  • Thanks! And great answer! – Bolli Nov 24 '14 at 07:12
  • Thanks Jack http://stackoverflow.com/users/1338292/ja%cd%a2ck nice answer , can it be sorted with the time stamp contains in the line aswell with same complexity with recent time first in order of CRITICAL-->Warning-->INFO – kkr Nov 25 '14 at 07:28
0

Getting CRITICAL on the sorted order

function my_cmp($a, $b){
 $pieces_a = explode("CRITICAL", $a);
 $pieces_b = explode("CRITICAL", $b);

 if(!isset($pieces_a[1]) && isset($pieces_b[1])) {
    return 1;
 }
 elseif(!isset($pieces_b[1]) && isset($pieces_a[1])) {
    return -1;
 }
 elseif(!isset($pieces_a[1]) && !isset($pieces_b[1])) {
    return 0;
 }
 return strcasecmp($pieces_a[1], $pieces_b[1]);
}
usort($arr, "my_cmp");

But this can only sort if the each line has non spaces I mean single word,.

Any other solution curious to know?

Bhumi Shah
  • 8,514
  • 5
  • 54
  • 88
Jack
  • 1