natural-sort

  1. natural-sort
    1. Introduction
    2. Author
    3. Repository
    4. API
      1. Customization
      2. Predicates
      3. Procedures
    5. Example
    6. License
    7. Version history
      1. 1.1
      2. 1.0
      3. 0.1

Introduction

There are situations where standard ASCII sorting does not suffice. This egg implements procedures for natural sorting as described on Coding Horror and the linked resources.

Author

Vasilij Schneidermann

Repository

https://depp.brause.cc/natural-sort

API

Customization

[parameter] (number-re)
[parameter] (number-re RE)

Parameter holding an irregex-compatible regular expression used to find a number. Defaults to (+ num), a sequence of at least one numerical digit.

[parameter] (natural-sort-preprocessor)
[parameter] (natural-sort-preprocessor PROC)

Parameter holding a preprocessing procedure that adjusts an input string before comparing it to another string. Defaults to identity to leave its input unchanged.

Predicates

[procedure] (natural-string<? STRING1 STRING2)
[procedure] (natural-string<=? STRING1 STRING2)
[procedure] (natural-string=? STRING1 STRING2)
[procedure] (natural-string<>? STRING1 STRING2)
[procedure] (natural-string>? STRING1 STRING2)
[procedure] (natural-string>=? STRING1 STRING2)

Predicates for comparing two strings according to natural sort order.

Procedures

[procedure] (natural-string-compare STRING1 STRING2)

Compares two strings according to natural sort order and returns -1 if STRING1 is smaller than STRING2, 0 if STRING1 is equal to STRING2 and 1 if STRING1 is greater than STRING2.

[procedure] (natural-sort STRINGS)

Convenience procedure for performing a natural sort on a list of strings. It is equivalent to (sort natural-string<? STRINGS).

Example

(import natural-sort)

(define (random-ipv4-address)
  (format "~a.~a.~a.~a" (random 256) (random 256) (random 256) (random 256)))

(for-each
 print
 (natural-sort
  (list-tabulate 10 (lambda (_) (random-ipv4-address)))))

;; 5.148.108.99
;; 41.117.237.105
;; 58.127.242.193
;; 94.166.18.142
;; 135.123.145.113
;; 136.203.161.99
;; 200.67.82.107
;; 228.135.148.232
;; 250.72.30.164
;; 250.132.51.35

License

Copyright (c) 2017, Vasilij Schneidermann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version history

1.1

1.0

0.1