Thursday, February 24, 2011

Ruby array sorting

I recently had to come up with a way to sort an array of objects that looked something like this,
  ["a", "c", "b", "1", "10", "3", "thing4", 
   "thing3", "thing30", "thing2"]

We needed the text only strings first and the strings with numbers in them sorted by their numbers. The example arrays explain it better. Here's what I came up with:


This will sort that array so it comes out looking like this:
  ["a", "b", "c", "1", "thing2", "3", "thing3", 
   "thing4", "10", "thing30"]

Pretty, huh?

No comments:

Post a Comment