multi_sort 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_sort.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ multi_sort (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.1.1)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.7)
16
+ rspec-expectations (2.14.5)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.6)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.3)
25
+ multi_sort!
26
+ rake
27
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Prabhat Thapa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # MultiSort
2
+
3
+ Many times I came across the requirements of sorting a array of hashes with one or more then one key's value of hash. I have wrote this gem which will accept the array of hashes as first parameter and array of keys to be sort by in a order.
4
+
5
+ Will add more features to this gem such as type of sorting, sorting of different type of multi-level hashes etc.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'multi_sort'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install multi_sort
20
+
21
+ ## Usage
22
+
23
+ ARR_HASH = [{ foo: 'a', bar: 4 },
24
+ { foo: 'b', bar: 3 },
25
+ { foo: 'c', bar: 1 },
26
+ { foo: 'b', bar: 4 }]
27
+
28
+ MultiSort.sort_by_order ARR_HASH, [:bar, :foo]
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/multi_sort.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "multi_sort/version"
2
+
3
+ module MultiSort
4
+ def self.sort_by_order hash = {}, order = []
5
+ hash.sort do |a,b|
6
+ a_arr = []
7
+ b_arr = []
8
+ order.each{|key| a_arr << a[key]; b_arr << b[key];}
9
+ a_arr <=> b_arr
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module MultiSort
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_sort/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_sort"
8
+ spec.version = MultiSort::VERSION
9
+ spec.authors = ["Prabhat Thapa"]
10
+ spec.email = ["prabhat4ever@gmail.com"]
11
+ spec.description = %q{Sort a array of hashes with one or multiple values}
12
+ spec.summary = %q{Sort a array of hashes with one or multiple values}
13
+ spec.homepage = "https://github.com/Gemathon-Lapidarists/multi_sort"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,40 @@
1
+ require 'multi_sort'
2
+ require 'spec_helper'
3
+
4
+ describe MultiSort do
5
+
6
+ it "It should return a blank array if both the params are blank" do
7
+ sort_arr_hash = MultiSort.sort_by_order [], []
8
+ sort_arr_hash.should == []
9
+ sort_arr_hash.size.should == 0
10
+ end
11
+
12
+ it "It should return a blank array if the array of hash is empty" do
13
+ sort_arr_hash = MultiSort.sort_by_order [], ['foo', 'bar']
14
+ sort_arr_hash.should == []
15
+ sort_arr_hash.size.should == 0
16
+ end
17
+
18
+ it "It should return a array of hash as it is with incorrect sort by keys" do
19
+ sort_arr_hash = MultiSort.sort_by_order ARR_HASH_ONE, ['alpha', 'tango']
20
+ sort_arr_hash.should == ARR_HASH_ONE
21
+ end
22
+
23
+ it "It should return a sorted array of hash as per the passed array of keys" do
24
+ sort_arr_hash = MultiSort.sort_by_order ARR_HASH_ONE, ['bar', 'foo']
25
+ sort_arr_hash.first.should == {"foo"=>2, "bar"=>1}
26
+ sort_arr_hash.last.should == {"foo"=>3, "bar"=>4}
27
+ end
28
+
29
+ it "It should return a sorted array of hash as per the passed array of symbolized keys" do
30
+ sort_arr_hash = MultiSort.sort_by_order ARR_HASH_TWO, [:foo, :bar]
31
+ sort_arr_hash[0].should == {:foo=>"a", :bar=>1}
32
+ sort_arr_hash[1].should == {:foo=>"a", :bar=>4}
33
+ end
34
+
35
+ it "It should return a sorted array of hash as per the single sorty by key" do
36
+ sort_arr_hash = MultiSort.sort_by_order ARR_HASH_TWO, [:foo]
37
+ sort_arr_hash.first.should == {:foo=>"a", :bar=>4}
38
+ sort_arr_hash.last.should == {:foo=>"d", :bar=>2}
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ ARR_HASH_ONE = [{ 'foo'=>3,'bar'=>4 },
2
+ { 'foo'=>2,'bar'=>3 },
3
+ { 'foo'=>1,'bar'=>2 },
4
+ { 'foo'=>4,'bar'=>1 },
5
+ { 'foo'=>2,'bar'=>1 },
6
+ { 'foo'=>1,'bar'=>3 },
7
+ { 'foo'=>2,'bar'=>4 }]
8
+
9
+ ARR_HASH_TWO = [{ foo: 'a', bar: 4 },
10
+ { foo: 'b', bar: 3 },
11
+ { foo: 'c', bar: 1 },
12
+ { foo: 'b', bar: 4 },
13
+ { foo: 'd', bar: 2 },
14
+ { foo: 'c', bar: 3 },
15
+ { foo: 'a', bar: 1 },]
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_sort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Prabhat Thapa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Sort a array of hashes with one or multiple values
63
+ email:
64
+ - prabhat4ever@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/multi_sort.rb
75
+ - lib/multi_sort/version.rb
76
+ - multi_sort.gemspec
77
+ - spec/multi_sort_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/Gemathon-Lapidarists/multi_sort
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Sort a array of hashes with one or multiple values
104
+ test_files:
105
+ - spec/multi_sort_spec.rb
106
+ - spec/spec_helper.rb