query
On this page

naturalSort

lib.naturalSort

Docs pulled from | This Revision | 10 minutes ago


Sort list using "Natural sorting". Numeric portions of strings are sorted in numeric order.

Inputs

lst

1. Function argument

Examples

lib.lists.naturalSort usage example

naturalSort ["disk11" "disk8" "disk100" "disk9"]
=> ["disk8" "disk9" "disk11" "disk100"]
naturalSort ["10.46.133.149" "10.5.16.62" "10.54.16.25"]
=> ["10.5.16.62" "10.46.133.149" "10.54.16.25"]
naturalSort ["v0.2" "v0.15" "v0.0.9"]
=> [ "v0.0.9" "v0.2" "v0.15" ]
(lib.lists.naturalSort)

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

naturalSort = lst:
    let
      vectorise = s: map (x: if isList x then toInt (head x) else x) (builtins.split "(0|[1-9][0-9]*)" s);
      prepared = map (x: [ (vectorise x) x ]) lst; # remember vectorised version for O(n) regex splits
      less = a: b: (compareLists compare (head a) (head b)) < 0;
    in
      map (x: elemAt x 1) (sort less prepared);