I'm trying to implement a paging algorithm for a dataset sortable via many criteria. Unfortunately, while some of those criteria can be implemented at the database level, some must be done at the app level (we have to integrate with another data source). We have a paging (actually infinite scroll) requirement and are looking for a way to minimize the pain of sorting the entire dataset at the app level with every paging call.
What is the best way to do a partial sort, only sorting the part of the list that absolutely needs to be sorted? Is there an equivalent to C++'s std::partial_sort
function available in the .NET libraries? How should I go about solving this problem?
EDIT: Here's an example of what I'm going for:
Let's say I need to get elements 21-40 of a 1000 element set, according to some sorting criteria. In order to speed up the sort, and since I have to go through the whole dataset every time anyway (this is a web service over HTTP, which is stateless), I don't need the whole dataset ordered. I only need elements 21-40 to be correctly ordered. It is sufficient to create 3 partitions: Elements 1-20, unsorted (but all less than element 21); elements 21-40, sorted; and elements 41-1000, unsorted (but all greater than element 40).
9,8,7,6,5,4,3,2,1
and you say Top 3: you really want to get1,2,3
. If the rest of the array is sorted, you don't care. Is that...sort of what you're after? – Solomon3, 2, 1
(unsorted, but all less than proper 4th element);4, 5, 6
(sorted and in the same place they would be for a sorted list);8, 7, 9
(unsorted, but all greater than proper 6th element). As you can see, the Top 3 is a special case of that (and same with the Bottom 3, for that matter). – Parklandvar someCollection = new[]{5,2,7,9,0,6,8}
create an emptyint[] sortCollection = {}
then from there create avar sortList = someCollection.ToList();
then sort thesortList.Sort()
then dosortCollection = sortList.ToArray();
now the sortCollection will have all your integer values sorted.. – Anzac