Project

deep_sort

0.0
No commit activity in last 3 years
No release in over 3 years
Sort Array/Hash/Enumerable recursively.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

deep_sort

Sort Array/Hash/Enumerable recursively.

Gem Version Build Status

Installation

Add this line to your application's Gemfile:

gem 'deep_sort'

And then execute:

$ bundle

Or install it yourself as:

$ gem install deep_sort

Methods

  • DeepSort.deep_sort(obj, options = {})
  • require 'deep_sort/extend'
    • Array#deep_sort(options = {})
    • Array#deep_sort!(options = {})
    • Hash#deep_sort(options = {})
    • Hash#deep_sort!(options = {})
    • Enumerable#deep_sort()

Usage

require 'deep_sort'
require 'pp'
require 'set'

hash = {"firstName"=>"John",
        "lastName"=>"Smith",
        "age"=>25,
        "address"=>
         {"streetAddress"=>"21 2nd Street",
          "city"=>"New York",
          "state"=>"NY",
          "postalCode"=>"10021"},
        "phoneNumber"=>
         [{"type"=>"home", "number"=>"212 555-1234"},
          {"type"=>"fax", "number"=>"646 555-4567"}],
        "gender"=>{"type"=>"male"},
        "number"=>[10, 5, 4, 9, 7],
        "tags"=>Set["staff", "engineer", {"joined_at"=>"2016", "emp_no"=>10}, "writer"]}

pp DeepSort.deep_sort(hash)
#=> {"address"=>
#     {"city"=>"New York",
#      "postalCode"=>"10021",
#      "state"=>"NY",
#      "streetAddress"=>"21 2nd Street"},
#    "age"=>25,
#    "firstName"=>"John",
#    "gender"=>{"type"=>"male"},
#    "lastName"=>"Smith",
#    "phoneNumber"=>
#     [{"number"=>"212 555-1234", "type"=>"home"},
#      {"number"=>"646 555-4567", "type"=>"fax"}],
#    "number" => [4, 5, 7, 9, 10],
#    "tags"=>Set["staff", "engineer", {"joined_at"=>"2016", "emp_no"=>10}, "writer"]}

sort_enum: true

pp DeepSort.deep_sort(sort_enum: true)
#=> {"address"=>
#     {"city"=>"New York",
#      "postalCode"=>"10021",
#      "state"=>"NY",
#      "streetAddress"=>"21 2nd Street"},
#    "age"=>25,
#    "firstName"=>"John",
#    "gender"=>{"type"=>"male"},
#    "lastName"=>"Smith",
#    "phoneNumber"=>
#     [{"number"=>"212 555-1234", "type"=>"home"},
#      {"number"=>"646 555-4567", "type"=>"fax"}],
#    "number" => [4, 5, 7, 9, 10],
#    "tags"=>["engineer", "staff", "writer", {"emp_no"=>10, "joined_at"=>"2016"}]}