chickadee » natural-sort

natural-sort

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://github.com/wasamasa/natural-sort

API

Customization

number-reparameter
number-re REparameter

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

natural-sort-preprocessorparameter
natural-sort-preprocessor PROCparameter

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

natural-string<? STRING1 STRING2procedure
natural-string<=? STRING1 STRING2procedure
natural-string=? STRING1 STRING2procedure
natural-string<>? STRING1 STRING2procedure
natural-string>? STRING1 STRING2procedure
natural-string>=? STRING1 STRING2procedure

Predicates for comparing two strings according to natural sort order.

Procedures

natural-string-compare STRING1 STRING2procedure

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.

natural-sort STRINGSprocedure

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

Example

(use 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

0.1

Contents »