/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/env ruby require 'osx/cocoa' # Build an array of Hashes to sort, could be objects, etc ... data = Array.new data << {:firstname => "Skippy", :lastname => "McFearson", :age => "31"} data << {:firstname => "Ned", :lastname => "Flanders", :age => "75"} data << {:firstname => "Jim", :lastname => "DodSon", :age => "44"} data << {:firstname => "Bob", :lastname => "Austin", :age => "25"} # Apply the Sort Descriptor and sort the array. ageDescriptor = OSX::NSSortDescriptor.alloc.initWithKey_ascending("age", true) sortDescriptors = [ageDescriptor] sortedArray = data.to_ns.sortedArrayUsingDescriptors(sortDescriptors) puts sortedArray.inspect