sort.htmTEXTBlWd JNJNZ Sorting

Back to index


GENERAL SORTING THEORY

Sorts in OOFILE are declarative - you specify a sort order and the database will appear in that order until you cancel or suspend the sort.

Re-sorting is lazily evaluated. It doesn't matter in which order you perform a succession of searches, selectXX or setSortOrder, the sorting only takes place at the last possible point.

This means, if you change a selection then the next start() or access to a field will trigger a re-sort. eg: searching, adding a record or deleting a record all make the sort "dirty".

The lazy evaluation means it is safe to perform a sequence like:
People.setSortOrder(People.OtherNames);
People.search(People.salary > 50000);
cout << People.count() << " people earn over $50,000" << endl;
People.searchSelection(People.LastName == "Dent");

if (People.count())
cout << "Dent family high-earners are: " << endl << People << endl;

The only re-sort that occurs is on the final cout ... << People.
The count() function has no need to access any data so does not trigger a re-sort.

CONTROLLING SORTS

You can control sorting with
People.suspendSorting(), resumeSorting() and unSorted().

Suspends and resumes are nested, and it is safe to call resumeSorting more times than a suspension has occurred.

If you want to force a sort to occur at a predictable time, you can trigger the sorter directly, eg:
People.sorter()->sortNow();

Be careful with code like the above, sorter() returns a dbSorter* and will return 0 if no sort is specified. Thus, you can tell if a sort has been specified by:

if (People.sorter())
blah;

SIMPLE SORTS

Simple single field sorts are specified by passing in a field, eg:
People.setSortOrder(People.LastName);
People.setReverseSortOrder(People.Salary);

MULTIPLE FIELD SORTS

Multiple field sorts require a dbSorter. This can be created in advance, and might even be a static member of your class or a local variable. This is useful if you want to reset the sort order several times, eg:

dbSorter() PeoplePaid;
PeoplePaid << People.LastName << reverse(People.Salary);
People.setSortOrder(PeoplePaid);
...
People.setSortOrder(People.LastName);

...
People.setSortOrder(PeoplePaid);

Creating the dbSorter on the fly is quite safe also, eg:
People.setSortOrder(dbSorter() << People.LastName << reverse(People.Salary));

USING COMPOUND FIELDS

If you will be sorting selections of all records, the fastest sort is using an indexed field. Thus, it may be worth the overhead of declaring a compound field that is indexed. Note that such fields incur a space penalty (for the extra index) and have a significant impact on the speed of adding records.

SORTING PROPAGATION

A dbSorter is a dependent of the dbTable that it sorts.

It responds to the following messages from the dbTable, in 3 cases:

whenever the current record is about to be changed by going to a new one or other existing record
OOFmsg_UnloadRecord & OOFmsg_ChangeContext
if (mNeedsToSort)
sortNow();

when the selection is replaced by an insert (importing data) or search
OOFmsg_ChangeSelection
mNeedsToSort = true;

when a record is saved, potentially changing the data it is sorted by and so affecting its order in the sorted selection
OOFmsg_SaveRecord

if (mSortOnSaves)
sortNow();

Back to index


 & &ZObject Master 3.0.4 Prefssort.htmrPartSIT!PartSIT!u %Realmz Preferences%ScriptWizard Preferences5%Sound Preferences   w%w%w%    w%w%w%  "a/Q  w%w%w%#  $  w%w%w%+  ,  w%w%w%7  8  w%w%w%o  |  w%w%w%   w%w%w%   w%w%w%   w%w%w%  A  w%w%w%D    w%w%w%    w%w%w%    w%w%w%  +  w%w%w%/  U  w%w%w%Y    w%w%w%    w%w%w%    w%w%w%    w%w%w%    w%w%w%  d  w%w%w%n    w%w%w%    w%w%w% %  w%w%w%/  L  w%w%w%P    w%w%w%    w%w%w%  g  w%w%w%k    w%w%w%  ,  w%w%w%1  E  w%w%w%I  R  w%w%w%[ g  w%w%w%q    w%w%w%    w%w%w%    w%w%w% )  w%w%w%3    w%w%w%   !  w%w%w% %  j  w%w%w% n   w%w%w%    w%w%w%    w%w%w%    w%w%w%    w%w%w%    w%w%w%   0  w%w%w% 4   w%w%w%    w%w%w%    w%w%w%     w%w%w%   B  w%w%w% G   w%w%w%    w%w%w%     w%w%w% #  7  w%w%w% ;  I  w%w%w% N   w%w%w%    w%w%w%    w%w%w%   F  w%w%w%J  \  w%w%w%`  b  w%w%w%f  w  w%w%w%{    w%w%w%    w%w%w%    w%w%w%    w%w%w%    w%w%w%    w%w%w%   & &Z0rZstylsoup&885922T2