Posted By

dbug13 on 03/09/09


Tagged

sort cocoa array ruby rubycocoa


Versions (?)


Advertising

Website Promotion DIRECTORY is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Who likes this?

1 person has marked this snippet as a favorite

webstic


RubyCocoa Apply NSSortDescriptor to a ruby array.


Published in: Ruby 






Expand | Embed | Plain Text
  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.

Download royalty free graphics