Skip to content

Instantly share code, notes, and snippets.

@cout
Created August 22, 2012 14:25
Show Gist options
  • Save cout/3426098 to your computer and use it in GitHub Desktop.
Save cout/3426098 to your computer and use it in GitHub Desktop.
nondeterministic behavior
s1 = [
{
"attributes" => {
"role"=>"grad-research-assistant",
},
"from" => "47baa997c443e40ae6519aae096d9e72",
"relationship_type" => "project_role"
},
{
"attributes" => {
"role" => "other - Student",
},
"from" => "179eae3982ab1e4047051381fbe0f137",
"relationship_type" => "project_role"
}
]
class Hash
def <=>(other)
result = ([ self.keys, self.values ] <=> [ other.keys, other.values ])
return result
end
end
def sort_relationships!(relationships)
relationships.sort! { |rel1, rel2|
[ rel1['relationship_type'], rel1.keys, rel1.values ] <=> [ rel2['relationship_type'], rel2.keys, rel2.values ]
}
end
sort_relationships!(s1)
# p s1
puts s1[0]['attributes']['role']
puts s1[1]['attributes']['role']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment