https://github.com/logandk/natural_sorting
Rails plugin implementing natural sorting of arrays
https://github.com/logandk/natural_sorting
Last synced: 8 months ago
JSON representation
Rails plugin implementing natural sorting of arrays
- Host: GitHub
- URL: https://github.com/logandk/natural_sorting
- Owner: logandk
- License: mit
- Created: 2009-03-28T17:06:28.000Z (about 17 years ago)
- Default Branch: master
- Last Pushed: 2009-04-09T19:12:41.000Z (about 17 years ago)
- Last Synced: 2025-08-31T20:49:09.972Z (10 months ago)
- Language: Ruby
- Homepage:
- Size: 81.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
- License: MIT-LICENSE
Awesome Lists containing this project
README
natural_sorting - Sort arrays in a more human way
=================================================
This is a Ruby on Rails plugin, which introduces a new method to the Array
class in order to sort values in a natural way.
Installation
============
As a Rails Plugin
-----------------
Use this to install as a plugin in a Ruby on Rails app:
$ script/plugin install git://github.com/logandk/natural_sorting.git
As a Rails Plugin (using git submodules)
----------------------------------------
Use this if you prefer the idea of being able to easily switch between using edge or a tagged version:
$ git submodule add git://github.com/logandk/natural_sorting.git vendor/plugins/natural_sorting
Usage
=====
The default `sort` method will sort your array like this:
>> ["Image 1", "Image 3", "Image 11", "Image 20"].sort
=> ["Image 1", "Image 11", "Image 20", "Image 3"]
This plugin introduces the `sort_natural` method, which sorts your array like this:
>> ["Image 1", "Image 3", "Image 11", "Image 20"].sort_natural
=> ["Image 1", "Image 3", "Image 11", "Image 20"]
Much better.
By default, the `sort_natural` method is not case-sensitive, but this can be controlled like this:
>> ["apple", "flaf", "Hello world!", "Arrrrr"].sort_natural :case_sensitive => true
=> ["Arrrrr", "Hello world!", "apple", "flaf"]
Also, the plugin adds a couple of other methods. Replace the array with the sorted version:
[...].sort_natural!
Compare two strings naturally, default case_sensitive = false:
String.natcmp("hello", "world"[, :case_sensitive => true])
Credits
=======
The `natcmp` method is based on the work by *Alan Davies*, as described in `lib/core_ext/string.rb`.
Copyright (c) 2008 Logan Raarup, released under the MIT license