class CRM_Utils_Sort {
/**
- * constants to determine what direction each variable
+ * Constants to determine what direction each variable
* is to be sorted
*
* @var int
CONST ASCENDING = 1, DESCENDING = 2, DONTCARE = 4,
/**
- * the name for the sort GET/POST param
+ * The name for the sort GET/POST param
*
* @var string
*/
SORT_ID = 'crmSID', SORT_DIRECTION = 'crmSortDirection', SORT_ORDER = 'crmSortOrder';
/**
- * name of the sort function. Used to isolate session variables
+ * Name of the sort function. Used to isolate session variables
* @var string
*/
protected $_name;
/**
- * array of variables that influence the query
+ * Array of variables that influence the query
*
* @var array
*/
public $_vars;
/**
- * the newly formulated base url to be used as links
+ * The newly formulated base url to be used as links
* for various table elements
*
* @var string
protected $_link;
/**
- * what's the name of the sort variable in a REQUEST
+ * What's the name of the sort variable in a REQUEST
*
* @var string
*/
}
/**
- * create the sortID string to be used in the GET param
+ * Create the sortID string to be used in the GET param
*
* @param int $index the field index
* @param int $dir the direction of the sort
}
/**
- * init the sort ID values in the object
+ * Init the sort ID values in the object
*
* @param string $defaultSortOrder the sort order to use by default
*
}
/**
- * init the object
+ * Init the object
*
* @param string $defaultSortOrder the sort order to use by default
*
}
/**
- * getter for currentSortID
+ * Getter for currentSortID
*
* @return int returns of the current sort id
* @acccess public
}
/**
- * getter for currentSortDirection
+ * Getter for currentSortDirection
*
* @return int returns of the current sort direction
* @acccess public
}
/**
- * Universal callback function for sorting by weight
+ * Universal callback function for sorting by weight, id, title or name
*
* @param $a
* @param $b
*
- * @return array of items sorted by weight
+ * @return int (-1 or 1)
* @access public
*/
static function cmpFunc($a, $b) {
- return ($a['weight'] <= $b['weight']) ? -1 : 1;
+ $cmp_order = array('weight', 'id', 'title', 'name');
+ foreach ($cmp_order as $attribute) {
+ if (isset($a[$attribute]) && isset($b[$attribute])) {
+ if ($a[$attribute] < $b[$attribute]) {
+ return -1;
+ } elseif ($a[$attribute] > $b[$attribute]) {
+ return 1;
+ } // else: $a and $b are equal wrt to this attribute, try next...
+ }
+ }
+ // if we get here, $a and $b es equal for all we know
+ // however, as I understand we don't want equality here:
+ return -1;
}
}
-