CodeIgniter Forums
What's the right place for sort algorithm? Model? View? Controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: What's the right place for sort algorithm? Model? View? Controller? (/showthread.php?tid=21520)



What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-12-2009

[eluser]Unknown[/eluser]
Hi!

I am new in MVC frameworks, so I have a question related to MVC paradigm.

I have SQL table with number of points with GPS data (lat/long).
And I have php algorithm (function) that can sort points by distance from any point.

My question is what is the correct place for that sort function?

1. May be model function should take parameters like $sort_by_point_lat $sort_by_point_long and sort these points
Code:
function get_points($sort_by_point_lat, $sort_by_point_long) {}

2. Or may by controller should sort them as it received from model
Code:
function show_points()
{
$my_points = $this->Points->get_points();
$this->sort_by_distance($my_points,123,456);
}

3. Or may be View should do it then it displays these points.


What do you think? Thanks!


What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-12-2009

[eluser]davidbehler[/eluser]
I would go with the first option and put it in the model file.
View and controller are definetly the wrong places as the view only displays the data it gets from the controller and the controller does all the logic between getting the data (from the model) and displaying it (in the view file).


What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-12-2009

[eluser]basementDUDE[/eluser]
[quote author="waldmeister" date="1250097415"]I would go with the first option and put it in the model file.
View and controller are definetly the wrong places as the view only displays the data it gets from the controller and the controller does all the logic between getting the data (from the model) and displaying it (in the view file).[/quote]

I believe model should only deal with database data, so get data sort out in the model is a good idea.


What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-13-2009

[eluser]renownedmedia[/eluser]
What if you want to use the same model in different controllers, and different controllers sort the data differently?


What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-13-2009

[eluser]rogierb[/eluser]
Sorting should be done in the model. There are two different approaches I use when dealing with sorting.
1: parse the order_by as a variable to you model
2: create different order_by's in you model and use one of them depending on a var you parse to the model.

I prever the last approach.


What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-13-2009

[eluser]Unknown[/eluser]
Thanks you all for answers! It very helpful.


What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-13-2009

[eluser]wabu[/eluser]
And don't forget if something's more complex you always have the option of isolating it in its own module/library/helper thingy.

But in this case it sounds relatively simple so my vote is for the model. Wink


What's the right place for sort algorithm? Model? View? Controller? - El Forum - 08-13-2009

[eluser]adamp1[/eluser]
I would say the actual sort code should go in a library. Then the model should use the library to be able to return the sorted data.