Yii Framework Forum: Sorting question - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Sorting question Rate Topic: -----

#1 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 23 March 2010 - 11:33 PM

My action method in the controller is as follows, nothing special.
        /**                                                                                                  
         * Shows a particular model.                                                                         
         */
        public function actionShow()
        {
                $criteria=new CDbCriteria;
                $criteria->order = 'task';

                $pages=new CPagination(Task::model()->count($criteria));
                $pages->pageSize=self::PAGE_SIZE;
                $pages->applyLimit($criteria);

                $sort=new CSort('Task');
                $sort->applyOrder($criteria);

                $models=Task::model()->findAll($criteria);

                $this->render('show',array(
                        'models'=>$models,
                        'pages'=>$pages,
                        'sort'=>$sort,
                ));
        }


I would like to sort the data($models) by task numbers which happen to be such as '1.1', '1.2', ... ,'1.9' and '1.10'.
So my question is how I would sort the task number using CSort (or some other way). I was not able to find the custom sort algorithm in CSort class.
Without a special care, the sort result might be like '1.1', '1.10', '1.2' ... Of course I prefer to have it sorted as '1.1', '1.2', ... , '1.9' and '1.10'.

Thank you in advance for your hint.
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 24 March 2010 - 05:27 AM

If in your db task are written like '1.1','1.2', '1.10', I have no idea about how to sort.

You have to immagine a sql order condition that allows you to correctly sort this elements, because CSORT cannot do anything that db is not able to do (CSORT simply add to the query an ORDER BY statement).

You can write in DB your task in 2 separated fields (like task_1, task_2) and $criteria->order = 'task_1, task_2'; will work properly.

You can add in model

public function getTask()
{
   return $this->task_1.'.'.$this->task_2
}


And noone will notice that the attribute task is changed!!! All "observer" views will work properly.

If you want to leave even the writer views unchanged you can write in model:

public function setTask($task)
{
   this->task_1=substring(...);
   $this->task_2=substring(...);
}

And even the view create and update task will never notice this change!
0

#3 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 24 March 2010 - 07:39 PM

Thanks zaccaria for your hint. Unfortunately, actual task numbers are like these; '1.1', '1.1.1, '1.1.1.1' etc. depending on the depth of the tasks.
I understand that CSort cannot handle sort algorithms; which is left to the database and the database generally is not wise enough to handle this.

One way is to limit the depth of the tasks to say ten. I am going to consider based on this idea, thanks again!
0

#4 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

Posted 25 March 2010 - 07:09 AM

I'm not sure, but wouldn't be possible to have an additional left-zero after dot like in '1.01', '1.02', '1.03', '1.04', ... '1.09', '1.10', ... '1.20' ... ?

this way the sorting is fluid
______________________________________
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 March 2010 - 07:18 AM

You could also check out MySQLs string functions and try to convert the '1.2.3' to an equivalent integer on DB side. You'll have to fiddle a little with integration into your model though (adding custom select fields).
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users