Sorting a CMap of objects

Does anyone have any idea how one can sort a CMap of objects based on a property of the object?

I tried using uasort but that will not accept CMap;




	protected static function compareBlocks($blockA, $blockB)

	{

		if($blockA instanceof LayoutBlock && $blockB instanceof LayoutBlock)

		{

			if($blockA->getWeight() === $blockB->getWeight())

			{

				return 0;

			}

			return ($blockA->getWeight() <= $blockB->getWeight()) ? -1 : 1;

		}else{

			throw new CException(Util::t('Both blocks must be instances of LayoutBlock or one of it\'s children.'));

		}

	}

	

	protected static function sortBlocks($blocks)

	{

		if($blocks instanceof CMap)

		{

			uasort($blocks, array(__CLASS__,'compareBlocks'));

		}

		return $blocks;

	}



I also tried to write a simple bubble sort function to do it, but I was having trouble there using the CMapIterator.

Any help is welcome.

It’s easiest to create a new CMap. You can use an intermediate PHP array to do the actual sorting:


$data=$oldMap->toArray()

$data=you_sort_method($data);

$newMap=new CMap($data);

Thank you.

prior to reading your post, I came up with




	protected static function compareBlocks($blockA, $blockB)

	{

		if($blockA instanceof LayoutBlock && $blockB instanceof LayoutBlock)

		{

			if($blockA->getWeight() === $blockB->getWeight())

			{

				return 0;

			}

			return ($blockA->getWeight() <= $blockB->getWeight()) ? -1 : 1;

		}else{

			throw new CException(Util::t('Both blocks must be instances of LayoutBlock or one of it\'s children.'));

		}

	}

	

	protected static function sortBlocks($blocks)

	{

		if($blocks instanceof CMap)

		{

			$blocks = Util::mapToArray($blocks);

		}

		

		uasort($blocks, array(__CLASS__,'compareBlocks'));

		

		return Util::arrayToMap($blocks);

	}



However, I’d like to change it to something more effecient.