public static function applySorting($pagelist, $sortOrder = null, $sortBy = null)
    {
        // sort order
        if (isset($sortOrder)) {
            switch ($sortOrder) {
                case 'asc':
                    $sortOrder = 1;
                    break;

                case 'desc':
                    $sortOrder = -1;
                    break;

                default:
                    $sortOrder = 1;
                    break;
            }
        }

        if (!isset($sortBy)) {
            $sortBy = 'name';
        }

        // sort key
        usort(
            $pagelist,
            function ($a, $b) use ($sortBy, $sortOrder) {
                if (isset($sortBy)) {
                    if (is_array($sortBy)) {
                        $key = key($sortBy);
                        $value = reset($sortBy);
                        if (isset($a->$key)) {
                            $temp = $a->$key;
                            if (isset($temp[$value])) {
                                $a = $temp[$value];
                            }
                        } else {
                            $a = 0;
                        }
                        if (isset($b->$key)) {
                            $temp = $b->$key;
                            if (isset($temp[$value])) {
                                $b = $temp[$value];
                            }
                        } else {
                            $b = 0;
                        }
                    } else {
                        $a = $a->{$sortBy};
                        $b = $b->{$sortBy};
                    }
                }

                if ($a == $b) {
                    return 0;
                }
                $compareResult = ($a < $b) ? 1 : -1;

                return $sortOrder * $compareResult;
            }
        );

        return $pagelist;
    }