Hey Cakes...!
This blog is abt sorting the fields of a custom array..
My array looks like this:
Array
(
[0] => Array
(
[Task] => Array
(
[id] => 2
[comment_id] => 2
[name] => Work Load
[feedback] => Half Done
[percentage_completed] => 50
[task_date] => 2009-05-17
)
[comment] => Array
(
[id] => 2
[name] => comment2
)
)
[1] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 5
[name] => Server Maintenance
[feedback] => Task in progress
[percentage_completed] => 60
[task_date] => 2010-07-31
)
[Comment] => Array
(
[id] => 5
[name] => server maintenance in progress
)
)
[2] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 6
[name] => Process Complianced
[feedback] => Almost Done
[percentage_completed] => 90
[task_date] => 2010-07-05
)
[Comment] => Array
(
[id] => 6
[name] => comment6
)
)
)
So here, i wanna sort [name] field of Task model.
To sort this array, i wil use Set::sort()
$somearray = Set::sort($somearray, '{n}.Task.name', 'desc');
This takes 3 arguments: The array to sort, Tthe array value to sort on, and the sorting order.
Here goes my Controller:
class TaskController extends AppController {
var $name = 'Task';
function index() {
$tasks = $this->Task->find('all');
$somearray = $tasks;
$somearray = Set::sort($somearray, '{n}.Task.name', 'desc');
$this->set('Task', $somearray);
}
}
?>
After Sorting, here is my sorted array..
Array
(
[0] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 6
[name] => Process Compliance
[feedback] => Almost Done
[percentage_completed] => 90
[task_date] => 2010-07-05
)
[comment] => Array
(
[id] => 6
[name] => comment6
)
)
[1] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 5
[name] => Server Maintenance
[feedback] => Task in progress
[percentage_completed] => 60
[task_date] => 2010-07-31
)
[Comment] => Array
(
[id] => 5
[name] => server maintenance in progress
)
)
[2] => Array
(
[Task] => Array
(
[id] => 2
[comment_id] => 2
[name] => Work Load
[feedback] => Half Done
[percentage_completed] => 50
[task_date] => 2009-05-17
)
[Comment] => Array
(
[id] => 2
[name] => comment2
)
)
)
Now, to sort an array like this:
Array
(
[0] => Array
(
[001] => Array
(
[id] => 2
[comment_id] => 2
[name] => Work Load
[feedback] => Half Done
[percentage_completed] => 50
[task_date] => 2009-05-17
)
[comment] => Array
(
[id] => 2
[alias] => comment2
)
)
[1] => Array
(
[002] => Array
(
[id] => 3
[comment_id] => 5
[name] => Server Maintenance
[feedback] => Task in progress
[percentage_completed] => 60
[task_date] => 2010-07-31
)
[Comment] => Array
(
[id] => 5
[alias] => server maintenance in progress
)
)
[2] => Array
(
[003] => Array
(
[id] => 3
[comment_id] => 6
[name] => Process Complianced
[feedback] => Almost Done
[percentage_completed] => 90
[task_date] => 2010-07-05
)
[Comment] => Array
(
[id] => 6
[alias] => comment6
)
)
)
Here is the technique:
class TaskController extends AppController {
var $name = 'Task';
function index() {
$tasks = $this->Task->find('all');
$somearray = $tasks;
$somearray = Set::sort($somearray, '{n}.{s}.name', 'desc');
$this->set('Task', $somearray);
}
}
?>
Sorting can also be done in find method or with paginate helper of Cakephp..
--
ಅರ್ಜುನ್ ಅರಸ್
This blog is abt sorting the fields of a custom array..
My array looks like this:
Array
(
[0] => Array
(
[Task] => Array
(
[id] => 2
[comment_id] => 2
[name] => Work Load
[feedback] => Half Done
[percentage_completed] => 50
[task_date] => 2009-05-17
)
[comment] => Array
(
[id] => 2
[name] => comment2
)
)
[1] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 5
[name] => Server Maintenance
[feedback] => Task in progress
[percentage_completed] => 60
[task_date] => 2010-07-31
)
[Comment] => Array
(
[id] => 5
[name] => server maintenance in progress
)
)
[2] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 6
[name] => Process Complianced
[feedback] => Almost Done
[percentage_completed] => 90
[task_date] => 2010-07-05
)
[Comment] => Array
(
[id] => 6
[name] => comment6
)
)
)
So here, i wanna sort [name] field of Task model.
To sort this array, i wil use Set::sort()
$somearray = Set::sort($somearray, '{n}.Task.name', 'desc');
This takes 3 arguments: The array to sort, Tthe array value to sort on, and the sorting order.
Here goes my Controller:
class TaskController extends AppController {
var $name = 'Task';
function index() {
$tasks = $this->Task->find('all');
$somearray = $tasks;
$somearray = Set::sort($somearray, '{n}.Task.name', 'desc');
$this->set('Task', $somearray);
}
}
?>
After Sorting, here is my sorted array..
Array
(
[0] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 6
[name] => Process Compliance
[feedback] => Almost Done
[percentage_completed] => 90
[task_date] => 2010-07-05
)
[comment] => Array
(
[id] => 6
[name] => comment6
)
)
[1] => Array
(
[Task] => Array
(
[id] => 3
[comment_id] => 5
[name] => Server Maintenance
[feedback] => Task in progress
[percentage_completed] => 60
[task_date] => 2010-07-31
)
[Comment] => Array
(
[id] => 5
[name] => server maintenance in progress
)
)
[2] => Array
(
[Task] => Array
(
[id] => 2
[comment_id] => 2
[name] => Work Load
[feedback] => Half Done
[percentage_completed] => 50
[task_date] => 2009-05-17
)
[Comment] => Array
(
[id] => 2
[name] => comment2
)
)
)
Now, to sort an array like this:
Array
(
[0] => Array
(
[001] => Array
(
[id] => 2
[comment_id] => 2
[name] => Work Load
[feedback] => Half Done
[percentage_completed] => 50
[task_date] => 2009-05-17
)
[comment] => Array
(
[id] => 2
[alias] => comment2
)
)
[1] => Array
(
[002] => Array
(
[id] => 3
[comment_id] => 5
[name] => Server Maintenance
[feedback] => Task in progress
[percentage_completed] => 60
[task_date] => 2010-07-31
)
[Comment] => Array
(
[id] => 5
[alias] => server maintenance in progress
)
)
[2] => Array
(
[003] => Array
(
[id] => 3
[comment_id] => 6
[name] => Process Complianced
[feedback] => Almost Done
[percentage_completed] => 90
[task_date] => 2010-07-05
)
[Comment] => Array
(
[id] => 6
[alias] => comment6
)
)
)
Here is the technique:
class TaskController extends AppController {
var $name = 'Task';
function index() {
$tasks = $this->Task->find('all');
$somearray = $tasks;
$somearray = Set::sort($somearray, '{n}.{s}.name', 'desc');
$this->set('Task', $somearray);
}
}
?>
Sorting can also be done in find method or with paginate helper of Cakephp..
--
ಅರ್ಜುನ್ ಅರಸ್
No comments:
Post a Comment