name = $_name; $this->price = $_price; $this->order = $_order; } } $arrayOfItems = array( new Item ("Glass", 3.44, 4), new Item ("Cup", 10.43, 2), new Item ("Plate", 9.02, 3), new Item ("Fork", 0.10, 1), new Item ("Spoon", 34.87, 5) ); usort($arrayOfItems, array("ItemSorter", "ComparisonDelegateByOrder")); PrintItemArray($arrayOfItems, "ComparisonDelegateByOrder"); usort($arrayOfItems, array("ItemSorter", "ComparisonDelegateByName")); PrintItemArray($arrayOfItems, "ComparisonDelegateByName"); usort($arrayOfItems, array("ItemSorter", "ComparisonDelegateByPrice")); PrintItemArray($arrayOfItems, "ComparisonDelegateByPrice"); function PrintItemArray($arrayOfItems, $delegateName) { echo "****************** $delegateName **********************"; foreach ($arrayOfItems as $key => $value) { echo "
"; print_r($value); echo ""; } } class ItemSorter { function ComparisonDelegateByOrder($a, $b) { return self::ComparisonDelegate($a, $b, "order"); } function ComparisonDelegateByName($a, $b) { return self::ComparisonDelegate($a, $b, "name"); } function ComparisonDelegateByPrice($a, $b) { return self::ComparisonDelegate($a, $b, "price"); } function ComparisonDelegate($a, $b, $field) { if ($a->$field == $b->$field) { return 0; } return ($a->$field < $b->$field) ? -1 : 1; } } //