|
"The best way to predict the future is to invent it." — Alan Kay |
|
Sort Criteria
by Peter William Lount peter@smalltalk.orgThe SortCriteria and SortCriteriaColumn (and SortCriteriaTest for an example) are the classes that are added to the Smalltalk system. No changes to SortedCollection were required or other Smalltalk system classes.

Copyright and Usage License
Contribute
Sort Criteria Test Example
With the following Sort Ordering (SortCriteriaTest test1):
| aSortedList aSortCriteria |
aSortCriteria := SortCriteria new.
aSortCriteria addColumnName: #code ascendingFlag: true.
aSortCriteria addColumnName: #size ascendingFlag: false.
aSortCriteria addColumnName: #length ascendingFlag: false.
aSortedList := SortedCollection sortBlock: aSortCriteria.
self addToList: aSortedList.
aSortedList printString
The results are (second and third columns are decending). Column Sort ordering is "code, size, and length".
SortedCollection ( (SortCriteriaTest code: 'R002' size: '18M' length: 65) (SortCriteriaTest code: 'R101' size: '26M' length: 90) (SortCriteriaTest code: 'R202' size: '29M' length: 70) (SortCriteriaTest code: 'R202' size: '15M' length: 114) (SortCriteriaTest code: 'R202' size: '15M' length: 89) (SortCriteriaTest code: 'R202' size: '15M' length: 89) (SortCriteriaTest code: 'R202' size: '15M' length: 45) (SortCriteriaTest code: 'R202' size: '15M' length: 18) (SortCriteriaTest code: 'R202' size: '15M' length: 16) (SortCriteriaTest code: 'R301' size: '5M' length: 50) )
With a slightly different sort ordering (SortCriteriaTest test2):
| aSortedList aSortCriteria |
aSortCriteria := SortCriteria new.
aSortCriteria addColumnName: #code ascendingFlag: true.
aSortCriteria addColumnName: #size ascendingFlag: true.
aSortCriteria addColumnName: #length ascendingFlag: true.
aSortedList := SortedCollection sortBlock: aSortCriteria.
self addToList: aSortedList.
aSortedList printString
The results are (all columns are ascending). Column Sort ordering is "code, size, and length".
SortedCollection ( (SortCriteriaTest code: 'R002' size: '18M' length: 65) (SortCriteriaTest code: 'R101' size: '26M' length: 90) (SortCriteriaTest code: 'R202' size: '15M' length: 16) (SortCriteriaTest code: 'R202' size: '15M' length: 18) (SortCriteriaTest code: 'R202' size: '15M' length: 45) (SortCriteriaTest code: 'R202' size: '15M' length: 89) (SortCriteriaTest code: 'R202' size: '15M' length: 89) (SortCriteriaTest code: 'R202' size: '15M' length: 114) (SortCriteriaTest code: 'R202' size: '29M' length: 70) (SortCriteriaTest code: 'R301' size: '5M' length: 50) )
With a slightly different sort ordering rearranging the columns so that "length" sorts first (SortCriteriaTest test3).
| aSortedList aSortCriteria |
aSortCriteria := SortCriteria new.
aSortCriteria addColumnName: #length ascendingFlag: false.
aSortCriteria addColumnName: #code ascendingFlag: true.
aSortCriteria addColumnName: #size ascendingFlag: true.
aSortedList := SortedCollection sortBlock: aSortCriteria.
self addToList: aSortedList.
aSortedList printString
The results are (first and second columns are ascending, the third column "length" is decending). Column sort sequence is "length, code, and size".
SortedCollection ( (SortCriteriaTest code: 'R202' size: '15M' length: 114) (SortCriteriaTest code: 'R101' size: '26M' length: 90) (SortCriteriaTest code: 'R202' size: '15M' length: 89) (SortCriteriaTest code: 'R202' size: '15M' length: 89) (SortCriteriaTest code: 'R202' size: '29M' length: 70) (SortCriteriaTest code: 'R002' size: '18M' length: 65) (SortCriteriaTest code: 'R301' size: '5M' length: 50) (SortCriteriaTest code: 'R202' size: '15M' length: 45) (SortCriteriaTest code: 'R202' size: '15M' length: 18) (SortCriteriaTest code: 'R202' size: '15M' length: 16) )
The method that creates the SortCrtieriaTest instances:
addToList: theSortedList
theSortedList add: (
SortCriteriaTest new
code: 'R301';
size: '5M';
length: 50;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R202';
size: '29M';
length: 70;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R002';
size: '18M';
length: 65;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R202';
size: '15M';
length: 89;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R101';
size: '26M';
length: 90;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R202';
size: '15M';
length: 16;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R202';
size: '15M';
length: 18;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R202';
size: '15M';
length: 45;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R202';
size: '15M';
length: 89;
yourself
).
theSortedList add: (
SortCriteriaTest new
code: 'R202';
size: '15M';
length: 114;
yourself
).
^theSortedList