Skip to main content

Partially Ordered Sort

An algorithm to sort a collection by a sequence of comparators.

Comparators

A comparator takes two elements a and b and returns

0 - if they are equal a positive number if a > b a negative number if a < b

For example the natural comparator on the integers is

def small_first(a, b):

return a - b

The Python sorted function takes a comparator function as a keyword input

>>> sorted([1, 5, 2, 4, 3], cmp=small_first)
[1, 2, 3, 4, 5]

We may choose to sort by different criteria. For example we might choose to prefer even numbers

def evens_first(a, b):

return a%2 - b%2

>>> sorted([1, 5, 2, 4, 3], cmp=evens_first)
[4, 2, 5, 1, 3]

Note in the example above that all of even numbers precede all of the odd numbers; evens_first is satisfied. Also note that there are several possible solutions. In this case there are 12 possible solutions that all satisfy the comparator function. sorted returns one at random.

posort

To obtain the solution [2, 4, 1, 3, 5] where numbers are sorted first by evenness and then by magnitude we can use the function posort.

The posort function attempts to satisfy a sequence of comparator functions of decreasing precedence.

>>> posort([1, 5, 2, 4, 3], evens_first, small_first)
[2, 4, 1, 3, 5]

The rule for even numbers dominates the rule for small numbers. Any number of comparator functions can be specified.

Equivalent structures

A comparator function completely specifies a [partial order](http://en.wikipedia.org/wiki/Partial_order#Formal_definition).

A comparator function can describe any [directed acyclic graph (DAG)](http://en.wikipedia.org/wiki/Directed_acyclic_graph)

Author

[Matthew Rocklin](http://matthewrocklin.com)

License

New BSD license. See LICENSE.txt

History

This idea was originally developed for the [Theano project](http://github.com/theano/theano)

Metadata

Release files for posort 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for posort 0.1
File Size Uploaded
posort-0.1.tar.gz 3.1 kB Details

Release files / posort-0.1.tar.gz

Download URL posort-0.1.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
607d5da848fcbe816c775cab932af06f7c3eb864a9df0e4f278fcea43ddc110a
BLAKE2b-256 checksum
How to use checksums
d7a0bf2d42a38c5eb5a552b9d1d9bc643b531dd57d5b500f888bae43ef5d1b18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page