How to quicksort over multiple columns

heddimbedleri

New Member
I'm looking to quicksort some objects in php.i'm sorting an array of OBJECTS\[code\]$object->x;$object->y;$object->z;\[/code\]I want to first sort by x, then y, then z.This is my quicksort functionWhere it accepts an array of jobjects, and sorts by a particular sortkey (x, y, or z column)The function returns a sorted array of objects, that have been sorted by the sortkey.\[code\]private function quicksort($objects, $sortKey) { if(count($objects) < 2) return $objects; $left = $right = array(); reset($objects); $pivot_key = key($objects); $pivot = array_shift($objects); foreach($objects as $k => $v) { if($v->$sortKey < $pivot->$sortKey) $left[$k] = $v; else $right[$k] = $v; } return array_merge($this->quicksort($left,$sortKey), array($pivot_key => $pivot), $this->quicksort($right,$sortKey));}\[/code\]I can easily quicksort any individual column using a quicksort recursive algorithm, but grouping them together and then sorting those subgroups to the nth time is really messing with my head.Is there an algorithm that I could be looking at?
 
Back
Top