Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

naturalsort

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

naturalsort

  • 1.3.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

NaturalSort

NaturalSort is a simple library which implements a natural, human-friendly alphanumeric sort in Ruby.

Examples

   %w[a1 a11 a12 a2 a21].natural_sort #=> %w[a1 a2 a11 a12 a21]
   %w[a b c A B C].natural_sort       #=> %w[A a B b C c]
   %w[x__2 x_1].natural_sort          #=> %w[x_1 x__2]
   %w[x2-y08 x2-g8 x2-y7 x8-y8].natural_sort   #=> %w[x2-g8 x2-y7 x2-y08 x8-y8]
   %w[x02-y08 x02-g8 x2-y7 x8-y8].natural_sort #=> %w[x02-g8 x2-y7 x02-y08 x8-y8]

Features

  • Sort case insensitive
  • Sort underscore insensitive
  • Sort filename matching patterns
  • Sort mixed alpha and numeric "abc1", "abc12", "abc2", "a1b2" in correct order

Install

With Bundler

In your Gemfile:

gem 'naturalsort'

or to optionally extend Ruby native objects:

gem 'naturalsort', require: 'natural_sort_kernel'
From Command Line
$ gem install naturalsort

Usage

Extend Ruby native enumerable objects

require 'natural_sort_kernel' adds natural_sort methods to all native Ruby enumerable objects (Array, Hash, etc...)

   require 'natural_sort_kernel'

   %w[a b c A B C].natural_sort
Use as a module function
   require 'natural_sort'  # unless using Bundler

   NaturalSort.sort %w[a b c d A B C D]
Use comparator function as a standalone

Adds natural_sort methods to Ruby native enumerable objects (Array, Hash, etc...)

   person_1 = Person.new('Moe')
   person_2 = Person.new('Larry')
   person_3 = Person.new('Curly')

   [person_1, person_2, person_3].sort{|a,b| NaturalSort.comparator(a.name, b.name)}  #=> [person_3, person_2, person_1]

   %w[a b c A B C].natural_sort
Include into your own objects

Can be used to add #natural_sort method to on any enumerable object or any object which implements #to_a

   class TodoList < Array
     include NaturalSort
   end

   todo_list = TodoList.new
   todo_list << 'Wash car'
   todo_list << 'Water plants'
   todo_list << 'Feed dog'

   todo_list.natural_sort #=> ['Feed dog', 'Wash car', 'Water plants']

Authors

  • Benjamin Francisoud
  • Johnny Shields (version 1.2.0+)

Contributing

Fork -> Patch -> Spec -> Push -> Pull Request

Links related to the natural sorting problem:

License

Copyright (c) 2007 Benjamin Francisoud

Licensed under the MIT License. Refer to LICENSE for details.

FAQs

Package last updated on 19 Oct 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc