RubyCocoa Apply NSSortDescriptor to a ruby array.


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.