GithubHelp home page GithubHelp logo

vast-community-hub / sortfunctions Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 112 KB

Collection sorting made easy with composable sort criteria that is easy to extend

License: MIT License

Smalltalk 100.00%
convenience smalltalk sorting vasmalltalk vastplatform

sortfunctions's Introduction

SortFunctions for VAST Platform

Introduction

SortFunctions provides a set of utility classes and extension methods that allow you to build composite sorting criteria using different collators without having to deal with complex block nesting and logic.

Examples

It enables you to perform sorting as follows:

"Sort people by lastName and then by firstName"
people sorted: #lastName ascending, #firstName descending.

"Convert the employees collection into a sorted collection
sorting elements by its salary, and then by its supervisor,
placing the employees without supervisor at the end (similar to SQL's NULLS LAST)."

employees asSortedCollection: #salary descending, [:each | each supervisor ] ascending undefinedLast

"Sort by salary and then whether each employee has a bonus."
employees asSortedCollection: #salary descending, #hasBonus asSortFunction

Installation

Using Tonel Support

Clone this repository in your local filesystem, load the ST: Tonel Support and ST: SUnit features in VAST 12 or newer, and then evaluate:

| loader |
loader := TonelLoader readFromPath: (CfsPath named: 'path-to-cloned-repo').
loader
	beUnattended;
	useGitVersion.
loader loadAllMapsWithRequiredMaps.

We provide convenience scripts for this in the scripts directory, both for importing from a Tonel repository and exporting to it.

Using exported binary apps

Load the ST: SUnit feature and then import and load the SortFunctions configuration map in the envy\SortFunctions.dat file in this repository.

Main concepts

Three way comparison

Collation is performed by means of doing a three way comparison where if an objectA is less than objectB the comparison will return -1, if they're equal it will return 0 and if objectB is greater than objectA it will return 1.

If you want to be able to compare objects within sort functions they need to be able to respond to the #threeWayCompareTo: message using the criteria described above.

Helper methods

Usually you don't have to instantiate these classes manually, and you create the different sort functions by means of sending #ascending, #descending or #asSortFunction (that is a synonym of #ascending) to either a Symbol or to a Block. These methods will return an instance of PropertySortFunction, which then you can chain with other sort functions by means of the #, message (this will instantiate a ChainedSortFunction), and then wrap them using a #reverse function and/or define the order for elements that peroperties that return nil using the #undefinedFirst or #undefinedLast messages.

Sort Functions as sort blocks

You can use a sort function, either a simple one, or a composed one as replacement for a SortedCollection sort block, as SortFunction implements the #value:value: method, making it polymorphic with a block.

If you convert a two-args block into a sort function (e.g. [:a :b | a <= b] asSortFunction) it will instantiate CollatorBlockFunction that will use the block as a collation function instead of performing a three way comparison.

Further reading

See the SortFunctionTest SUnit class to see the different combinations you can use.

History

The original concept was written by written by Travis Griggs and then ported to Pharo first by Nicolas Cellier and then ported again by Esteban A. Maringolo, who added tests and some other sort functions.

When SortFunctions became part of the Pharo kernel, it received some curation and modifications, most notably the replacement of the "spaceship operator" <=> selector with the #threeWayCompareTo: keyword selector.

sortfunctions's People

Contributors

emaringolo avatar marianopeck avatar

Watchers

 avatar Seth Berman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.