Posted By

dbug13 on 03/09/09


Tagged

sort cocoa array ruby rubycocoa


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


RubyCocoa Apply NSSortDescriptor to a ruby array.


 / Published in: Ruby
 

  1. #!/usr/bin/env ruby
  2. require 'osx/cocoa'
  3.  
  4. # Build an array of Hashes to sort, could be objects, etc ...
  5. data = Array.new
  6. data << {:firstname => "Skippy", :lastname => "McFearson", :age => "31"}
  7. data << {:firstname => "Ned", :lastname => "Flanders", :age => "75"}
  8. data << {:firstname => "Jim", :lastname => "DodSon", :age => "44"}
  9. data << {:firstname => "Bob", :lastname => "Austin", :age => "25"}
  10.  
  11. # Apply the Sort Descriptor and sort the array.
  12. ageDescriptor = OSX::NSSortDescriptor.alloc.initWithKey_ascending("age", true)
  13. sortDescriptors = [ageDescriptor]
  14. sortedArray = data.to_ns.sortedArrayUsingDescriptors(sortDescriptors)
  15.  
  16. puts sortedArray.inspect

Report this snippet  

You need to login to post a comment.