mcrossen / deepsort Goto Github PK
View Code? Open in Web Editor NEWA ruby gem that adds recursive sorting functionality to hashes and arrays.
License: MIT License
A ruby gem that adds recursive sorting functionality to hashes and arrays.
License: MIT License
it would be useful to add a feature that allows hashes to be deep merged. duplicate keys wouldn't be overwritten, the values would be merged.
Ruby 1.8.7
#!/usr/bin/ruby
require 'pp'
require 'rubygems'
require 'deepsort'
nested = {"z" => 4, "b" => 3, "a"=>[2, 1] }
nested.deep_sort
pp nested # sorts in place without the `!`?
nested = {"z" => 4, "b" => 3, "a"=>[2, 1] }
pp nested.deep_sort # prints the original array
Result:
{"a"=>[2, 1], "b"=>3, "z"=>4}
{"z"=>4, "b"=>3, "a"=>[1, 2]}
I have some code that serializes both Hash
and Array
objects as YAML.
I had originally written:
data = data.deep_stringify_keys if data.respond_to?(:deep_stringify_keys)
data = data.deep_sort if data.respond_to?(:deep_sort)
This is fine for Hash
, since the YAML spec specifies that order in mappings is a serialization detail.
But of course deepsort
also adds deep_sort
to Array
, which is not a serialization detail.
I ended up changing the code to check .is_a?(Hash)
data = data.deep_sort if data.is_a?(Hash)
but it would have been nice if there were a way to only patch Hash
and not Array
when using deepsort
.
this may require adding map! and each! to Hash in a clever way
I'd prefer to use Deepsort.sort(hash)
... would you accept a PR to make this available ?
... what are you thoughts about making that the default and having a separate file like deepsort/ext
do the patching ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.