Jump to content

Reverse Natural Sort issue


lemaster777
Go to solution Solved by requinix,

Recommended Posts

http://php.net/manual/en/book.simplexml.php

Yes, I saw the manual - no __set_state() in the list of methods

 

 

If you re-arrange your array, so that the id to sort on is the key, then you could do the same user sort on that key.

Link to comment
Share on other sites

If I remove the references to that method, this seems to work

$data = array ( 0 => (array( 'DOCID' => '96651', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2015-102 ', )), 1 => (array( 'DOCID' => '96646', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCTITLE' => 'Order No. 3430 - Order Approving Modification One', 'DOCKETID' => 'CP2015-106 ', )), 2 => (array( 'DOCID' => '96645', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2015-114 ', )), 3 => (array( 'DOCID' => '96648', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2015-75 ', )), 4 => (array( 'DOCID' => '96649', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2015-76 ', )), 5 => (array( 'DOCID' => '96650', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2015-86 ', )), 6 => (array( 'DOCID' => '96652', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2015-93 ', )), 7 => (array( 'DOCID' => '96640', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2016-237 ', )), 8 => (array( 'DOCID' => '96641', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2016-238 ', )), 9 => (array( 'DOCID' => '96643', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2016-241 ', )), 10 => (array( 'DOCID' => '96653', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2016-242 ', )), 11 => (array( 'DOCID' => '96647', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2016-246 ', )), 12 => (array( 'DOCID' => '96644', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'CP2016-3 ', )), 13 => (array( 'DOCID' => '96641', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'MC2016-164 ', )), 14 => (array( 'DOCID' => '96643', 'DATERECD' => '2016-07-15T00:00:00-04:00', 'DOCKETID' => 'MC2016-167 ', )), );

usort($data, function($a,$b) {
    list($a1,$a2,$a3) = splitParts($a['DOCKETID']);
    list($b1,$b2,$b3) = splitParts($b['DOCKETID']);
    $x = strcmp($a1,$b1);
    if ($x == 0) {
        $y = $b2 - $a2;
        if ($y == 0) {
            return $b3 - $a3;
        }
        return $y;
    }
    return $x;
});


      
function splitParts($s) {
    $k = strlen($s);
    $a = ['','',''];
    $i = 0;
    $p = 0;
    while ($i < $k) {
        $c = $s[$i++];
        if (ctype_digit($c) && $p==0) $p = 1;
        if ($c=='-') {
            $c = $s[$i++];
            ++$p;
        }
        $a[$p] .= $c;
    }
    return $a;
}

print_r ($data); 

giving

Array
(
    [0] => Array
        (
            [DOCID] => 96647
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2016-246 
        )

    [1] => Array
        (
            [DOCID] => 96653
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2016-242 
        )

    [2] => Array
        (
            [DOCID] => 96643
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2016-241 
        )

    [3] => Array
        (
            [DOCID] => 96641
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2016-238 
        )

    [4] => Array
        (
            [DOCID] => 96640
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2016-237 
        )

    [5] => Array
        (
            [DOCID] => 96644
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2016-3 
        )

    [6] => Array
        (
            [DOCID] => 96645
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2015-114 
        )

    [7] => Array
        (
            [DOCID] => 96646
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCTITLE] => Order No. 3430 - Order Approving Modification One
            [DOCKETID] => CP2015-106 
        )

    [8] => Array
        (
            [DOCID] => 96651
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2015-102 
        )

    [9] => Array
        (
            [DOCID] => 96652
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2015-93 
        )

    [10] => Array
        (
            [DOCID] => 96650
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2015-86 
        )

    [11] => Array
        (
            [DOCID] => 96649
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2015-76 
        )

    [12] => Array
        (
            [DOCID] => 96648
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => CP2015-75 
        )

    [13] => Array
        (
            [DOCID] => 96643
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => MC2016-167 
        )

    [14] => Array
        (
            [DOCID] => 96641
            [DATERECD] => 2016-07-15T00:00:00-04:00
            [DOCKETID] => MC2016-164 
        )

)
Edited by Barand
Link to comment
Share on other sites

Ok, first---- Thanks for all the help.

Second, I have the following:

I get the data into an array and then apply your function. I must need to sort or pass in one of the array columns.  I do not get an error, but I also do not get the result.

$table should contain the data from XML.

I then put it into an Array and apply the function:

foreach($table as $case) {

  $sortable[] = $case;

}

 

var_dump($sortable);  'I HAVE ATTACHED THE RESULTS TO THIS

usort($sortable,function($a,$b) {

  list($a1, $a2, $a3) = splitParts($a);

  list($b1, $b2, $b3) = splitParts($b);

  $x = strcmp($a1, $b1);

  if ($x == 0) {

   $y = $b2 - $a2;

  if ($y == 0) {

  return $b3 - $a3;

}

return $y;

}

return $x;

});

 

'NOW I LOOP THROUGH $sortable to display results

foreach($sortable as $case)

{

  'write out results to a table.........

}

 

DailyVarDump.txt

Link to comment
Share on other sites

This is what the API they have is returning. It returns object from simplexml. That's all I can give you, below is the function that generates this:

 

function GetDailyListingSummary($filterdate) {

//ini_set('soap.wsdl_cache_enable', '0');

$x = new SoapClient(variable_get('prcwebservice'),variable_get('soapauth'));

// make an API call and get data

$result = $x->GetDailyListingSummary(array('docDate'=>$filterdate));

// Interpret the XML response

$apiresult = simplexml_load_string($result->GetDailyListingSummaryResult->any);

$dockets = $apiresult->DailyListingSummary->DailyListingData;

 

 

 

 

 

return $dockets;

}

Link to comment
Share on other sites

in the file you posted earlier (Fill_Id.txt) you have this line

$xml = simplexml_load_string($ParseData);

So it looks as though $parseData is the XML. Do you have anything similar for this new bit of data?

 

 

Similarly, the code you just posted, the input to simplexml_load_string() is

 

$result->GetDailyListingSummaryResult->any

 

That should be the XML.

Link to comment
Share on other sites


Ok, here is where I stand, and I think I am over the initial roadblock.

1. I have the simplexml into an array. This is working.

$sortable = array();
foreach ($table as $case) {
$sortable[]=$case;
}

usort($sortable, function($a, b$) {
//I DID THIS TO TEST. The sort was not seeing the $a, $b as a string
//so I did the following
$test1 = (string)$a->DOCKETID; // This got me just the element we want to sort on
$test2 = (string)$b->DOCKETID; // This got me just the element we want to sort on

list($a1, $a2, $a3) = splitParts($test1); // Calling the function you provided below
list($b1, $b2, $b3) = splitParts($test2);

$x = strcmp($a1, $b1);
if ($x == 0) {
$y = $b2 - $a2;
if ($y == 0) {
return $b3 - $a3;
}
return $y;
}
return $x;
});

function splitParts($s) {
$k = strlen($s);
$a = ['','',''];
$i = 0;
$p = 0;
while ($i < $k) {
$c = $s[$i++];
if (ctype_digit($c) && $p==0) $p = 1;
if ($c=='-') {
$c = $s[$i++];
++$p;
}
$a[$p] .= $c;
}
return $a;
}

NOW - The last piece of my confusion. The results we want this time are as below. The original sort is what we wanted in my initial request, and that worked great. The piece I'm stuck on now is to sort differently, as below.
What we want:
CP2015-75
CP2015-76
CP2015-86
CP2015-93
CP2015-102
CP2015-106
CP2015-114
CP2016-3
CP2016-237
CP2016-238
CP2016-241
CP2016-242
CP2016-246
MC2016-164
MC2016-167


What we are getting:
CP2016-246
CP2016-242
CP2016-241
CP2016-238
CP2016-237
CP2016-3
CP2015-114
CP2015-106
CP2015-102
CP2015-93
CP2015-86
CP2015-76
CP2015-75
MC2016-167
MC2016-164


Link to comment
Share on other sites

Uhg..  Ok, learning more as I go.  If I just switch the $y=$b2 - $a2; around to $y = $a2 - $b2; then it works.

 

 

I can not thank you enough for helping me muddle through this.  Not having access to the original authors source code for webservice api and other portions of this site, combined with my lack of php knowledge is making this a great adventure.

 

Thanks again.

Link to comment
Share on other sites

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.