{"id":13880080,"url":"https://github.com/dogweather/naturally","last_synced_at":"2025-04-04T09:09:12.409Z","repository":{"id":5849049,"uuid":"7065608","full_name":"dogweather/naturally","owner":"dogweather","description":"Natural sort algorithm","archived":false,"fork":false,"pushed_at":"2023-10-17T00:02:12.000Z","size":83,"stargazers_count":87,"open_issues_count":7,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-26T14:36:21.184Z","etag":null,"topics":["array-comparison","ruby","sort","sort-order"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dogweather.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-12-08T08:42:47.000Z","updated_at":"2024-06-04T09:29:34.000Z","dependencies_parsed_at":"2024-06-02T22:24:59.673Z","dependency_job_id":null,"html_url":"https://github.com/dogweather/naturally","commit_stats":{"total_commits":106,"total_committers":14,"mean_commits":7.571428571428571,"dds":"0.21698113207547165","last_synced_commit":"c884159e88a48bbea258d62818a21aee588adbaf"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dogweather%2Fnaturally","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dogweather%2Fnaturally/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dogweather%2Fnaturally/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dogweather%2Fnaturally/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dogweather","download_url":"https://codeload.github.com/dogweather/naturally/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245868266,"owners_count":20685607,"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":["array-comparison","ruby","sort","sort-order"],"created_at":"2024-08-06T08:02:46.445Z","updated_at":"2025-03-28T08:07:40.820Z","avatar_url":"https://github.com/dogweather.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Naturally\n[![Gem Version](https://badge.fury.io/rb/naturally.svg)](https://badge.fury.io/rb/naturally)\n[![Build Status](https://travis-ci.com/dogweather/naturally.svg?branch=master)](https://travis-ci.com/dogweather/naturally)\n[![Maintainability](https://api.codeclimate.com/v1/badges/0ebf4ef97723f2622105/maintainability)](https://codeclimate.com/github/dogweather/naturally/maintainability)\n\nNatural (\"version number\") sorting with support for:\n\n* **legal document numbering**,\n* **college course codes**, and\n* **Unicode**.\n\nSee Jeff Atwood's [Sorting for Humans: Natural Sort Order](https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/) and my post [Counting to 10 in Californian](https://blog.public.law/2012/08/07/counting-from-1-to-10-in-californian/).\n\n## Installation\n\n```Shell\n$ gem install naturally\n```\n\n## Usage\n\n```Ruby\nrequire 'naturally'\n\n# Sort a simple array of strings with legal numbering\nNaturally.sort([\"336\", \"335a\", \"335\", \"335.1\"])  # =\u003e [\"335\", \"335.1\", \"335a\", \"336\"]\n\n# Sort version numbers\nNaturally.sort([\"13.10\", \"13.04\", \"10.10\", \"10.04.4\"])  # =\u003e [\"10.04.4\", \"10.10\", \"13.04\", \"13.10\"]\n```\n\nUsually the library is used to sort an array of objects:\n\n\n```Ruby\n# Define a new simple object for storing Ubuntu versions\nUbuntuVersion = Struct.new(:name, :version)\n\n# Create an array\nreleases = [\n  UbuntuVersion.new('Saucy Salamander', '13.10'),\n  UbuntuVersion.new('Raring Ringtail',  '13.04'),\n  UbuntuVersion.new('Precise Pangolin', '12.04.4'),\n  UbuntuVersion.new('Maverick Meerkat', '10.10'),\n  UbuntuVersion.new('Quantal Quetzal',  '12.10'),\n  UbuntuVersion.new('Lucid Lynx',       '10.04.4')\n]\n\n# Sort by version number\nsorted = Naturally.sort(releases, by: :version)\n\n# Check what we have\nexpect(sorted.map(\u0026:name)).to eq [\n  'Lucid Lynx',\n  'Maverick Meerkat',\n  'Precise Pangolin',\n  'Quantal Quetzal',\n  'Raring Ringtail',\n  'Saucy Salamander'\n]\n```\n\n[More examples are in the specs](https://github.com/dogweather/naturally/blob/master/spec/naturally_spec.rb).\n\n\n## Implementation Notes\n\nThe algorithm capitalizes on Ruby's array comparison behavior:\nSince each dotted number actually represents a hierarchical \nidentifier, [array comparison](http://ruby-doc.org/core-2.2.1/Array.html#method-i-3C-3D-3E) \nis a natural fit:\n\n\u003e Arrays are compared in an “element-wise” manner; the first element of ary is compared with the first one of other_ary using the \u003c=\u003e operator, then each of the second elements, etc… As soon as the result of any such comparison is non zero (i.e. the two corresponding elements are not equal), that result is returned for the whole array comparison.\n\n\nAnd so, when given input such as,\n\n```ruby\n['1.9', '1.9a', '1.10']\n```\n\n...this module sorts the segmented numbers \nby comparing them in their array forms:\n\n```ruby\n[['1', '9'], ['1', '9a'], ['1', '10']]\n```\n\nFinally, upon actual sort comparison, each of these strings is \nconverted to an array of typed objects. This is to determine the \nsort order between heterogenous (yet ordered) segments such as \n`'9a'` and `'9'`.\n\nThe final nested comparison structure looks like this:\n\n```ruby\n  [\n   [\n     [1], [9]\n   ],\n   [\n     [1], [9, 'a']\n   ],\n   [\n     [1], [10]\n   ]\n  ]\n```\n\n## Related Work\n\n* [ahoward/version_sorter](https://github.com/ahoward/version_sorter), the starting point for the `naturally` gem.\n* [GitHub's Version sorter](https://github.com/github/version_sorter)\n\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdogweather%2Fnaturally","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdogweather%2Fnaturally","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdogweather%2Fnaturally/lists"}