Smalltalk.org™ | ZokuTalk™ Lisp, Smalltalk, + Others Hybrid
Smalltalk.org



SortCritter aka Sort Criteria for Multi Column Sorting with SortedCollections
by Peter William Lount, 20190628
Copyright 1996-2019, all rights reserved.

The Sort Criteria objects extend the standard SortCollection with a simple yet powerful dynamic multicolumn sorting capability.

The Sort Criteria objects extend the standard SortCollection with an easy to use, simple yet powerful and dynamic sorting capability that can sort multiple 'columns' or 'attributes' of a collection of objects. It's easy to rearrange the column sort ordering and the acending or decending state of the sort. It's simple because it just works with SortedCollections as you currently use them. It's powerful because you can sort any sequence of attributes in an object. It's dynamic as a SortCriteria and it's columns can be created on the fly by a user interface enabling the user to choose their own sort order and ascending/decending sequence.

The 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.

An example of constructing a Multi Column sort using the SortCriteria.
| aSortedList aSortCriteria |
aSortCriteria := SortCriteria new.
aSortCriteria addColumnName: #lastName ascendingFlag: true.
aSortCriteria addColumnName: #firstName ascendingFlag: true.
aSortCriteria addColumnName: #city ascendingFlag: false.

aSortedList := SortedCollection sortBlock: aSortCriteria.
That's it. Now your sorted collection, presumably with some kind of PersonInfo object in it, will sort according to the lastName, firstName, and city in the ascending or decending order you picked.

If it won't work in your Smalltalk the first thing to check is if the SortCriteria class implements all the methods required to pretend to be a Block, are all the methods that SortedCollection sends to the sortBlock implemented on SortCriteria? Some Smalltalks have one or two extra methods or different methods.

License: BSD, GPL, MIT.

Downloads:
SortCriter_GNUSmalltalk.st
SortCriteria_Squeak_v1.st

Sort Critter at Squeak Map.