 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
jannie00
Joined: 07 Apr 2008 Posts: 6
|
Posted: Wed Apr 09, 2008 5:15 pm Post subject: Datasource Array not sorting on multiple columns |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
|
| Back to top |
|
 |
jannie00
Joined: 07 Apr 2008 Posts: 6
|
Posted: Thu Apr 10, 2008 9:30 am Post subject: |
|
|
| Hmm, OK, did not see that one. Will look better next time... sorry and thanx. |
|
| Back to top |
|
 |
jannie00
Joined: 07 Apr 2008 Posts: 6
|
Posted: Thu Apr 10, 2008 2:03 pm Post subject: |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Thu Apr 10, 2008 6:38 pm Post subject: |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Tue May 06, 2008 9:39 pm Post subject: |
|
|
| 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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Fri May 23, 2008 8:33 pm Post subject: |
|
|
| 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 |
|
 |
jannie00
Joined: 07 Apr 2008 Posts: 6
|
Posted: Wed Aug 20, 2008 1:05 pm Post subject: |
|
|
Works like a charm here! Thanks.
Regards, Chris |
|
| Back to top |
|
 |
|
|
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
|
|