{"id":15286896,"url":"https://github.com/wevre/natural-compare","last_synced_at":"2025-04-13T03:57:13.816Z","repository":{"id":62435150,"uuid":"280458338","full_name":"wevre/natural-compare","owner":"wevre","description":"Natural sort comparator for strings in Clojure/Script.","archived":false,"fork":false,"pushed_at":"2024-06-24T03:48:26.000Z","size":43,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T03:57:08.666Z","etag":null,"topics":["clojure","clojurescript","comparator","natural-sort","sorting"],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wevre.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-17T15:20:27.000Z","updated_at":"2024-06-24T18:11:26.000Z","dependencies_parsed_at":"2022-11-01T21:02:37.845Z","dependency_job_id":null,"html_url":"https://github.com/wevre/natural-compare","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wevre%2Fnatural-compare","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wevre%2Fnatural-compare/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wevre%2Fnatural-compare/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wevre%2Fnatural-compare/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wevre","download_url":"https://codeload.github.com/wevre/natural-compare/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661707,"owners_count":21141450,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clojure","clojurescript","comparator","natural-sort","sorting"],"created_at":"2024-09-30T15:18:53.389Z","updated_at":"2025-04-13T03:57:13.792Z","avatar_url":"https://github.com/wevre.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# natural-compare\n\n[![cljdoc badge](https://cljdoc.org/badge/wevre/natural-compare)](https://cljdoc.org/d/wevre/natural-compare)\n[![bb compatible](https://raw.githubusercontent.com/babashka/babashka/master/logo/badge.svg)](https://babashka.org)\n\nA natural-sort [comparator](https://clojure.org/guides/comparators) for strings\nin Clojure/Script. Treats embedded digits as integers, so strings like `[\"v12\"\n\"v2\"]` will receive a natural sort, as opposed to a lexical sort:\n\n```clj\n(def v [\"v12\" \"v2\"])\n\n(sort v)\n;;=\u003e (\"v12\" \"v2\")   ;; lexical sort\n\n(sort natural-compare v)\n;;=\u003e (\"v2\" \"v12\")   ;; natural sort\n```\n\n# Install\n\ndeps.edn\n\n    wevre/natural-compare {:mvn/version \"0.0.10\"}\n\nproject.clj\n\n    [wevre/natural-compare \"0.0.10\"]\n\nOr, and this might be the easiest, just copy the body of\n`impl/natural_compare_parse.cljc` directly into your project.\n\n# How to use\n\n```clj\n(require '[wevre.natural-compare :refer [natural-compare])\n\n(def ss [\"t3\" \"t1\" \"t10\" \"t12\" \"t2\" \"t27\"])\n\n(sort natural-compare ss)\n;;=\u003e (\"t1\" \"t2\" \"t3\" \"t10\" \"t12\" \"t27\")\n\n(sort (comp - natural-compare) ss)\n;;=\u003e (\"t27\" \"t12\" \"t10\" \"t3\" \"t2\" \"t1\")\n\n```\n\n# How it works\n\n`natural-compare` operates on strings. Each is split into a sequence of\nalternating text and integer elements (always starting with a text element) and\nthen the two sequences are compared element by element. The elements are padded\nas necessary with values that always sort lower than 'legit' values, to ensure\nshorter strings sort first.\n\nSee test cases for more examples.\n\n# Where it came from\n\nAdapted from a gist (and subsequent comments) by Wilker Lúcio -- [`Alphabetical/Natural sorting in\nClojure/Clojurescript`](https://gist.github.com/wilkerlucio/db54dc83a9664124f3febf6356f04509)\n\nReading those comments, you'll see there is some discussion about limitations\n(overflow) and performance. This library actually contains a few different\nimplementations taken from that discussion. Based on my rudimentary benchmarks\nso far, I have drawn these conclusions:\n\n* For shorter strings, the implementations based on parsing integers are\n  fastest, but they have the potential limitation to overflow on (string\n  representations of) large integer inputs.\n\n* The \"lazy\" implementation outperforms the others on large strings where it can\n  stop early without needing to split and parse the entire string.\n\n* It depends on your use case, but I feel that most situations where a natural\n  sort order is desired are about small strings (like filenames or labels), so\n  the concerns about overflow or laziness are (in my experience) not as\n  important.\n\n* The default implemenation used in this library (\"parse\") is based on parsing\n  integers and will overflow. If you need something different for your use case,\n  check out one of the other implementations.\n\n# What's next\n\nSomething I would consider useful, but haven't needed yet in my own projects:\nproperly sort strings that are date names, like \"MON\", \"TUE\", …; \"JAN\", \"FEB\",\n….\n\n\n\n# License\n\nCopyright © Mike Weaver\n\nLicensed under the terms of Ecplise Public License 2.0, see license.txt.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwevre%2Fnatural-compare","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwevre%2Fnatural-compare","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwevre%2Fnatural-compare/lists"}