Skip to main content

orderby

Python key functions for multi-field ordering in SQL ORDER BY fashion

https://img.shields.io/pypi/v/orderby.svg

Meant to be used with built-in sorted() key function.

Supports also list.sort() doing in-place sorting.

Implementation uses operator.itemgetter() + some internal helper classes to allow descending sorting order.

So far this is tested and used on lists of dictionaries. Adding support for named tuples and others would be possible (using operator.attrgetter()).

Usage

  • SQL-like: orderby('foo ASC, bar DESC')

  • chained: asc('foo').desc('bar') usage

  • multiple fields at once: asc('foo', 'bar')

Examples

orderby() string syntax:

>>> from orderby import orderby
>>> import json
>>> files = [
...   {'size': 1234, 'path': 'foo/bar.txt'},
...   {'size': 0, 'path': '/dev/null'},
...   {'size': 1234, 'path': 'foo/abc.bin'},
... ]
>>> print(json.dumps(sorted(files, key=orderby('size DESC, path')), indent=2))
[
  {
    "size": 1234,
    "path": "foo/abc.bin"
  },
  {
    "size": 1234,
    "path": "foo/bar.txt"
  },
  {
    "size": 0,
    "path": "/dev/null"
  }
]

Chained asc() and desc() usage:

>>> from orderby import asc, desc
>>> print(json.dumps(sorted(files, key=desc('size').asc('path')), indent=2))
[
  {
    "size": 1234,
    "path": "foo/abc.bin"
  },
  {
    "size": 1234,
    "path": "foo/bar.txt"
  },
  {
    "size": 0,
    "path": "/dev/null"
  }
]

In-place sorting of a list:

>>> files.sort(key=desc('path'))
>>> print(json.dumps(files, indent=2))
[
  {
    "size": 1234,
    "path": "foo/bar.txt"
  },
  {
    "size": 1234,
    "path": "foo/abc.bin"
  },
  {
    "size": 0,
    "path": "/dev/null"
  }
]
>>> files.sort(key=desc('size').asc('path'))
>>> print(json.dumps(files, indent=2))
[
  {
    "size": 1234,
    "path": "foo/abc.bin"
  },
  {
    "size": 1234,
    "path": "foo/bar.txt"
  },
  {
    "size": 0,
    "path": "/dev/null"
  }
]

Internals

To be explained here later…

Release files for orderby 0.0.2

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

Source distribution (sdist)

Source distribution for orderby 0.0.2
File Size Uploaded
orderby-0.0.2.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for orderby 0.0.2
File Interpreter ABI Platform
orderby-0.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 11.5 kB

Release files / orderby-0.0.2.tar.gz

Download URL orderby-0.0.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
26f5892356f79855c1369f47a17328b8dcbf7452e2459c1651c91929b8edbd6c
BLAKE2b-256 checksum
How to use checksums
9ccb2228287acb2c54d235359929a8b9885cc8aa94e7ecaaa2227a83e7eb04fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / orderby-0.0.2-py2.py3-none-any.whl

Download URL orderby-0.0.2-py2.py3-none-any.whl
Size 7.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a6e712acc0539edbfe3b610963a171988a98aead226d262ccb4b924f642e8d53
BLAKE2b-256 checksum
How to use checksums
7def987515469ece2f6c85a06d73f3d725b065fe533073218d1d17875dcfb5e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

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