frankvanlaere Posted May 26, 2006 Share Posted May 26, 2006 Hello there,I have a class (let's call it Mojo) which has values like id, position etc.This class then gets initialized with:Mojo austin1, austin2, austin3;Now, how do I sort _all_ three classes on the value position? This value can change and at certain points they have to be sorted on that variable.I've been trying stuff like sort(Mojo->position); but it doesn't seem to work. I could fix this by inserting them into a temporary table into a mysql database, and then re-fetching them in the correct order, but that's not very good is it? And I feel that there must be an easier solution.Thanks!ps. I know I haven't used the word classes correctly all the time, either way, there's only 1 class and when I say classes I mean those austin1, austin2, austin3 things, what do you call them? Instances? Quote Link to comment https://forums.phpfreaks.com/topic/10545-solved-sorting-several-classes/ Share on other sites More sharing options...
hvle Posted May 27, 2006 Share Posted May 27, 2006 [!--quoteo(post=377455:date=May 27 2006, 09:45 AM:name=TheScorpion)--][div class=\'quotetop\']QUOTE(TheScorpion @ May 27 2006, 09:45 AM) [snapback]377455[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hello there,I have a class (let's call it Mojo) which has values like id, position etc.This class then gets initialized with:Mojo austin1, austin2, austin3;Now, how do I sort _all_ three classes on the value position? This value can change and at certain points they have to be sorted on that variable.I've been trying stuff like sort(Mojo->position); but it doesn't seem to work. I could fix this by inserting them into a temporary table into a mysql database, and then re-fetching them in the correct order, but that's not very good is it? And I feel that there must be an easier solution.Thanks!ps. I know I haven't used the word classes correctly all the time, either way, there's only 1 class and when I say classes I mean those austin1, austin2, austin3 things, what do you call them? Instances?[/quote]Best way is putting them in an array.let say: MojoArr = array();MojoArr['austin1'] = new Mojo();MojoArr['austin2'] = new Mojo();MojoArr['austin3'] = new Mojo();ksort(MojoArr); // will sort in accending order: austin1,2,3krsort(MojoArr);// will sort in descending order: austin3,2,1you got the idea. Quote Link to comment https://forums.phpfreaks.com/topic/10545-solved-sorting-several-classes/#findComment-39384 Share on other sites More sharing options...
Barand Posted May 27, 2006 Share Posted May 27, 2006 If you define the mojo class with position as the first class variable in the definition you can put them in an array and just sort the array.Example[code]class mojo { var $position; var $id; function mojo ($id) { $this->id = $id; $this->position = rand(1,10); }}$austin = array();for ($i=1; $i <= 3; $i++) { $austin[] = new mojo($i);}sort ($austin);echo '<pre>', print_r($austin, true), '</pre>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10545-solved-sorting-several-classes/#findComment-39410 Share on other sites More sharing options...
frankvanlaere Posted May 31, 2006 Author Share Posted May 31, 2006 Thanks! :) Quote Link to comment https://forums.phpfreaks.com/topic/10545-solved-sorting-several-classes/#findComment-40443 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.