You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP

natural_sort

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

natural_sort

0.3.0
100

Supply Chain Security

100

Vulnerability

100

Quality

100

Maintenance

100

License

Version published
Maintainers
1
Created

Natural Sort

Build Status Gem Version Code Climate

Natual sorting implementation in Ruby.

Installation

Add this line to your application's Gemfile:

gem "natural_sort"

And then execute:

$ bundle

Or install it yourself as:

$ gem install natural_sort

Usage

list = ["a10", "a", "a20", "a1b", "a1a", "a2", "a0", "a1"]
list.sort(&NaturalSort) # => ["a", "a0", "a1", "a1a", "a1b", "a2", "a10", "a20"]
list = ["a10", "a", "a20", "a1b", "a1a", "a2", "a0", "a1"]
NaturalSort.sort(list)  # => ["a", "a0", "a1", "a1a", "a1b", "a2", "a10", "a20"]
list = ["a10", "a", "a20", "a1b", "a1a", "a2", "a0", "a1"]
NaturalSort.sort! list  # => ["a", "a0", "a1", "a1a", "a1b", "a2", "a10", "a20"]
list  # => ["a", "a0", "a1", "a1a", "a1b", "a2", "a10", "a20"]
UbuntuRelease = Struct.new(:number, :name)

ubuntu_releases = [
  UbuntuRelease.new("9.04", "Jaunty Jackalope"),
  UbuntuRelease.new("10.10", "Maverick Meerkat"),
  UbuntuRelease.new("8.10", "Intrepid Ibex"),
  UbuntuRelease.new("10.04.4", "Lucid Lynx"),
  UbuntuRelease.new("9.10", "Karmic Koala"),
]

ubuntu_releases.sort_by { |v| NaturalSort(v.number) }
# => [
#   UbuntuRelease.new("8.10", "Intrepid Ibex"),
#   UbuntuRelease.new("9.04", "Jaunty Jackalope"),
#   UbuntuRelease.new("9.10", "Karmic Koala"),
#   UbuntuRelease.new("10.04.4", "Lucid Lynx"),
#   UbuntuRelease.new("10.10", "Maverick Meerkat")
# ]

Refinements

If you're running ruby 2.1 or newer, you can use refinements.

require "natural_sort/refinments"

class MyClass
  using NaturalSort

  list.natural_sort                         # => ["a", "a0", "a1", "a1a"...
  ubuntu_releases.natural_sort_by(&:number) # => [ UbuntuRelease.new("8.10"...
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rwz/natural_sort.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 27 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts