private static void SetAnchor <TKey, TValue>(List <KeyValuePair <TKey, TValue> > list, PagingPredicate <TKey, TValue> pagingPredicate, int nearestPage)
        {
            if (list.Count == 0)
            {
                return;
            }

            int size     = list.Count;
            int pageSize = pagingPredicate.GetPageSize();

            for (int i = pageSize; i <= size; i += pageSize)
            {
                KeyValuePair <TKey, TValue> anchor = list[i - 1];
                nearestPage++;
                pagingPredicate.SetAnchor(nearestPage, anchor);
            }
        }
        public static SortedQueryResultSet <TKey, TValue> GetSortedQueryResultSet <TKey, TValue>(ISet <KeyValuePair <TKey, TValue> > list,
                                                                                                 PagingPredicate <TKey, TValue> pagingPredicate, IterationType iterationType)
        {
            if (list == null || list.Count == 0)
            {
                return(new SortedQueryResultSet <TKey, TValue>());
            }
            IComparer <KeyValuePair <TKey, TValue> > comparer   = SortingUtil.NewComparator(pagingPredicate.GetComparator(), iterationType);
            List <KeyValuePair <TKey, TValue> >      sortedList = new List <KeyValuePair <TKey, TValue> >();

            sortedList.AddRange(list);
            sortedList.Sort(comparer);

            KeyValuePair <int, KeyValuePair <TKey, TValue> > nearestAnchorEntry = pagingPredicate.GetNearestAnchorEntry();
            int nearestPage = nearestAnchorEntry.Key;
            int page        = pagingPredicate.GetPage();
            int pageSize    = pagingPredicate.GetPageSize();
            int begin       = pageSize * (page - nearestPage - 1);
            int size        = sortedList.Count;

            if (begin > size)
            {
                return(new SortedQueryResultSet <TKey, TValue>());
            }
            int end = begin + pageSize;

            if (end > size)
            {
                pageSize = size - begin;
            }

            SetAnchor(sortedList, pagingPredicate, nearestPage);
            List <KeyValuePair <TKey, TValue> > subList = sortedList.GetRange(begin, pageSize);

            return(new SortedQueryResultSet <TKey, TValue>(comparer, subList, iterationType));
        }