protected function sort(array $array, array $args)
    {
        $c = count($args);

        // PHP 5.3 hack
        $self = $this;

        usort($array, function ($a, $b) use ($self, $args, $c) {
            $i   = 0;
            $cmp = 0;
            while ($cmp == 0 && $i < $c) {
                $keyName = $args[$i][0];
                if($keyName == 'id' || $keyName == '__id') {
                    $valueA = $a->getId();
                    $valueB = $b->getId();
                } else {
                    $valueA = $self->getFieldValue($a, $keyName, $found);
                    if ($found === false) {
                        $valueA = null;
                    }
                    $valueB = $self->getFieldValue($b, $keyName, $found);
                    if ($found === false) {
                        $valueB = null;
                    }
                }

                if (is_string($valueA)) {
                    $cmp = strcmp($valueA, $valueB);
                } elseif (is_bool($valueA)) {
                    $cmp = $valueA - $valueB;
                } else {
                    $cmp = ($valueA == $valueB) ? 0 : (($valueA < $valueB) ? -1 : 1);
                }

                if ($args[$i][1] === SORT_DESC) {
                    $cmp *= -1;
                }
                $i++;
            }

            return $cmp;
        });

        return $array;
    }