PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Datasource Array not sorting on multiple columns

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> Structures
View previous topic :: View next topic  
Author Message
jannie00



Joined: 07 Apr 2008
Posts: 6

PostPosted: Wed Apr 09, 2008 5:15 pm    Post subject: Datasource Array not sorting on multiple columns Reply with quote

Maybe I miss something but I cannot get an Array datasource to sort on multiple columns. In the following code the output is only sorted on 'surname' and not on 'first name':

Code:
$test = array(
    array('id' => '1', 'first name' => 'John', 'surname' => 'Williams'),
    array('id' => '2', 'first name' => 'William', 'surname' => 'Williams'),
    array('id' => '3', 'first name' => 'William', 'surname' => 'Smith'),
    array('id' => '4', 'first name' => 'Bert', 'surname' => 'Williams')
    );
require 'Structures/DataGrid.php';
$dataGrid =& new Structures_DataGrid();
$dataGrid->setDefaultSort(array(
    'surname' => 'ASC',
    'first name' => 'DESC'
));
$dataGrid->bind($test, array(), 'Array');
$dataGrid->generateColumns(array(
    'first name' => 'First name',
    'surname' => 'Last name'
    ));
$dataGrid->render();


The output is:

First name - Last name
William - Smith
John - Williams
William - Williams
Bert - Williams

It is only sorted on the 'Last name' column and not on the 'First name' column.
Is this a restiction on the Array datasource? When I use a MDB2 datasource it works correct on multiple columns.

Regards, Chris
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1011

PostPosted: Wed Apr 09, 2008 6:34 pm    Post subject: Reply with quote

http://pear.php.net/manual/en/package.structures.structures-datagrid.structures-datagrid-datasource.array.php
(Table 61-1)

=> yes, it is a restriction currently
Back to top
View user's profile Send private message
jannie00



Joined: 07 Apr 2008
Posts: 6

PostPosted: Thu Apr 10, 2008 9:30 am    Post subject: Reply with quote

Hmm, OK, did not see that one. Will look better next time... sorry and thanx.
Back to top
View user's profile Send private message
jannie00



Joined: 07 Apr 2008
Posts: 6

PostPosted: Thu Apr 10, 2008 2:03 pm    Post subject: Reply with quote

Since I needed the feature I added it to the Array datasource. I rewrote the sort function:

Code:
function sort($sortField, $sortDir = null)
{
    $args = array();
    foreach ($sortField as $field => $order) {
        $fields = array();
        foreach ($this->_ar as $key => $row) {
            $value = $this->_options['natsort']
                ? strtolower($row[$field]) : $row[$field];
            $fields[$field][$key] = $value;
        }
        $args[] = $fields[$field];
        $args[] = (strtoupper($order) == 'DESC') ? SORT_DESC : SORT_ASC;
    }
    // Reference the final argument, otherwise it won't be sorted for some
    // particular reason.
    $args[] =& $this->_ar;
    call_user_func_array('array_multisort', $args);
}


Maybe it can be done more efficiently, but it works for me (for now).

Do not forget to add the following in the constructor:

Code:
$this->_setFeatures(array('multiSort' => true));


Feel free to add it to the Array datasource in the official PEAR distribution.

Regards, Chris
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1011

PostPosted: Thu Apr 10, 2008 6:38 pm    Post subject: Reply with quote

Thanks for the suggested change. I'll try to take a deeper look on the weekend into it.

If you want to be credited for this patch in the changelog, please send me your full name (either here, via private message, via email, via PEAR's bug tracker, ...).
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1011

PostPosted: Tue May 06, 2008 9:39 pm    Post subject: Reply with quote

Chris, sorry for the delay. I've found finally some time to review your changes on the weekend. Unfortunately, this works only for very simple cases (this might be enough for your code, of course). Making multisort working for all cases (especially all tests of our test suite) isn't easy, and therefore, we won't release the array driver with multisorting in the near future. But the idea and the request won't be forgotten, and I'd like to say thanks for posting the change here. Maybe it already helps other people with similiar demands.
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1011

PostPosted: Fri May 23, 2008 8:33 pm    Post subject: Reply with quote

Chris, FYI: multisorting was implemented today for the Array DataSource driver by Olivier. If you want, please get the new code from CVS and test it.
Back to top
View user's profile Send private message
jannie00



Joined: 07 Apr 2008
Posts: 6

PostPosted: Wed Aug 20, 2008 1:05 pm    Post subject: Reply with quote

Works like a charm here! Thanks.

Regards, Chris
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> Structures All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software