Page 1 of 1

Sorting an array of classes

Posted: Thu Apr 08, 2004 12:08 pm
by theWondering
Hi there!

I have a class:

Code: Select all

class Player
	{
		var $name;
		var $kills = 0;
		var $deaths = 0;
		var $rank = 0;
		var $weapon = array("THOMPSON"=>0,"MP40"=>0,""=>0,"DYNAMITE"=>0,"GRENADE_LAUNCHER"=>0,
							"BROWNING"=>0,"MACHINEGUN"=>0,"FLAMETHROWER"=>0,"PANZERFAUST"=>0,
							"FG42"=>0,"FG42SCOPE"=>0,"COLT"=>0,"LUGER"=>0,"LANDMINE"=>0,"ARTY"=>0,
							"MORTAR"=>0,"M7"=>0,"CARBINE"=>0,"AIRSTRIKE"=>0,"STEN"=>0,"K43"=>0,
							"AKIMBO_COLT"=>0,"AKIMBO_LUGER"=>0,"MG42"=>0,"MOBILE_MG42"=>0,
							"KAR98"=>0,"KNIFE"=>0,"GPG40"=>0,"SATCHEL"=>0,"CRUSH_CONSTRUCTION"=>0,
							"GARAND"=>0,"GARAND_SCOPE"=>0,"SILENCER"=>0,"K43_SCOPE"=>0,
							"AKIMBO_SILENCEDLUGER"=>0,"SILENCED_COLT"=>0,"AKIMBO_SILENCEDCOLT"=>0);

		function dominant_weapon()
		{
			$res = 0;
			foreach($this->weapon as $tot)
			{
				if($tot > $res)
				{
					$res = $tot;
				}
			}
			return array_search($res,$this->weapon);
		}
	}
Now, during the parsing of my file, I create an arry of this class in a loop thats like so:

Code: Select all

$plї$player_count] = new Player;
					$plї$player_count]->name = $matchї1];

					$plї$player_count]->kills += 1;
					$player_count += 1;
Now, what I would like to do is SORT this $pl array by ->kills How the HECK would I do something like this? I have tried everything under the sun :-(

Thanks

Posted: Thu Apr 08, 2004 12:12 pm
by magicrobotmonkey
I don't think you will be able to use any built in sort thing you'll have to do your own!

Yeah

Posted: Thu Apr 08, 2004 12:19 pm
by theWondering
Thats what Im thinking, but how?

Posted: Thu Apr 08, 2004 12:25 pm
by magicrobotmonkey
Wait, are you pulling these from a DB, look up ORDERBY !

Posted: Thu Apr 08, 2004 1:45 pm
by feyd

Posted: Thu Apr 08, 2004 2:20 pm
by magicrobotmonkey
Wow, PHP amazes me again! What didn't they think of?

Posted: Thu Apr 08, 2004 2:26 pm
by feyd
I'd like to see labels and goto statements myself.