Return to Snippet

Revision: 12277
at March 9, 2009 00:05 by dbug13


Initial Code
#!/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

Initial URL

                                

Initial Description

                                

Initial Title
RubyCocoa Apply NSSortDescriptor to a ruby array.

Initial Tags
sort, array, ruby

Initial Language
Ruby