sort_collections 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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +1 -0
- data/lib/sort_collections.rb +6 -0
- data/lib/sort_collections/array.rb +36 -0
- data/lib/sort_collections/version.rb +3 -0
- data/sort_collections.gemspec +23 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d31dc78ad5668603494f69501365498573306554
|
4
|
+
data.tar.gz: 1f4b2950c5c980f60ef5bc0c418804e80a99c7e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7faba016eb02ef2638d9229917c90f27f1cef4c9172cd77fa1316fda4efc5007155fbfe0b274a2b5d57da3209991b72cb41138d156f996f59c6026b7eeec7962
|
7
|
+
data.tar.gz: 4b0269a94178cc1e465cae98e71e6f8829e16527c357fa9b71447fceb04155021ca1cd7d2a61206edede0066e70fbb0efe225bac63bc49196d2f535835c251e8
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 DylanDeng
|
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,85 @@
|
|
1
|
+
# SortCollections
|
2
|
+
|
3
|
+
To sort a array that the element is `Hash`
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sort_collections'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sort_collections
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
# init sort data
|
23
|
+
hash_array = [{"created_at"=>"Fri, 31 May 2013 00:26:53 HKT +08:00",
|
24
|
+
"email"=>"peop@example.edu",
|
25
|
+
"id"=>3,
|
26
|
+
"updated_at"=>"Fri, 31 May 2013 00:27:21 HKT +08:00"},
|
27
|
+
{"created_at"=>"Thu, 30 May 2013 18:53:12 HKT +08:00",
|
28
|
+
"email"=>"coco@example.com",
|
29
|
+
"id"=>2,
|
30
|
+
"updated_at"=>"Sat, 06 Jul 2013 17:25:13 HKT +08:00"},
|
31
|
+
{"created_at"=>"Sun, 16 Jun 2013 11:23:42 HKT +08:00",
|
32
|
+
"email"=>"lele@example.com",
|
33
|
+
"id"=>5,
|
34
|
+
"updated_at"=>"Tue, 23 Jul 2013 15:44:46 HKT +08:00"},
|
35
|
+
{"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
|
36
|
+
"email"=>"bebe@example.com",
|
37
|
+
"id"=>4,
|
38
|
+
"updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
|
39
|
+
{"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
|
40
|
+
"email"=>"bebe@example.com",
|
41
|
+
"id"=>4,
|
42
|
+
"updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
|
43
|
+
{"created_at"=>"Thu, 30 May 2013 11:19:11 HKT +08:00",
|
44
|
+
"email"=>"admin@example.com",
|
45
|
+
"id"=>1,
|
46
|
+
"updated_at"=>"Wed, 11 Sep 2013 15:13:25 HKT +08:00"}]
|
47
|
+
|
48
|
+
# default: order_type: "asc", order_with: "id"
|
49
|
+
hash_array.order_collection
|
50
|
+
|
51
|
+
# assign order_with, e.g.: "email"
|
52
|
+
hash_array.order_collection(order_with: "email")
|
53
|
+
|
54
|
+
# ==> Result:
|
55
|
+
|
56
|
+
[{"created_at"=>"Thu, 30 May 2013 11:19:11 HKT +08:00",
|
57
|
+
"email"=>"admin@example.com",
|
58
|
+
"updated_at"=>"Wed, 11 Sep 2013 15:13:25 HKT +08:00"},
|
59
|
+
{"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
|
60
|
+
"email"=>"bebe@example.com",
|
61
|
+
"updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
|
62
|
+
{"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
|
63
|
+
"email"=>"bebe@example.com",
|
64
|
+
"updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
|
65
|
+
{"created_at"=>"Thu, 30 May 2013 18:53:12 HKT +08:00",
|
66
|
+
"email"=>"coco@example.com",
|
67
|
+
"updated_at"=>"Sat, 06 Jul 2013 17:25:13 HKT +08:00"},
|
68
|
+
{"created_at"=>"Sun, 16 Jun 2013 11:23:42 HKT +08:00",
|
69
|
+
"email"=>"lele@example.com",
|
70
|
+
"updated_at"=>"Tue, 23 Jul 2013 15:44:46 HKT +08:00"},
|
71
|
+
{"created_at"=>"Fri, 31 May 2013 00:26:53 HKT +08:00",
|
72
|
+
"email"=>"peop@example.edu",
|
73
|
+
"updated_at"=>"Fri, 31 May 2013 00:27:21 HKT +08:00"}]
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
You can assign any key word what you want!
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
1. Fork it
|
82
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
84
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def order_collection(options = {})
|
4
|
+
return self if self.empty?
|
5
|
+
if first.is_a?(Hash)
|
6
|
+
return order_hash(self, options)
|
7
|
+
end
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def order_hash(array, options={})
|
13
|
+
order_type = options[:order_type] || "asc"
|
14
|
+
order_with = options[:order_with] || "id"
|
15
|
+
|
16
|
+
# to collect 'order_with' values for all
|
17
|
+
order_elements = array.collect{|element| element[order_with]}
|
18
|
+
order_type == "asc" ? order_elements.sort! : order_elements.sort!{ |x, y| y <=> x }
|
19
|
+
|
20
|
+
array_to_hash = {}
|
21
|
+
array_times = array.length.times
|
22
|
+
array_times.each {|index| array_to_hash[index] = array[index]}
|
23
|
+
|
24
|
+
after_order_array = []
|
25
|
+
order_elements.each do |element|
|
26
|
+
array_to_hash.each do |key, value|
|
27
|
+
if value[order_with].to_s == element.to_s
|
28
|
+
after_order_array << value
|
29
|
+
array_to_hash.delete(key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
after_order_array
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sort_collections/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sort_collections"
|
8
|
+
spec.version = SortCollections::VERSION
|
9
|
+
spec.authors = ["Dylan Deng"]
|
10
|
+
spec.email = ["dylan@beansmile.com"]
|
11
|
+
spec.description = "To sort a array that the element is `Hash`"
|
12
|
+
spec.summary = "To sort a array that the element is `Hash`"
|
13
|
+
spec.homepage = "https://github.com/lanrion/sort_collections"
|
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 "rspec", "~> 2.6"
|
22
|
+
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sort_collections
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dylan Deng
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.6'
|
27
|
+
description: To sort a array that the element is `Hash`
|
28
|
+
email:
|
29
|
+
- dylan@beansmile.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- lib/sort_collections.rb
|
40
|
+
- lib/sort_collections/array.rb
|
41
|
+
- lib/sort_collections/version.rb
|
42
|
+
- sort_collections.gemspec
|
43
|
+
homepage: https://github.com/lanrion/sort_collections
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.0.6
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: To sort a array that the element is `Hash`
|
67
|
+
test_files: []
|