Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Fjölnir �sgeirsson http://ninjakitten.us

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Alphabetical sorter

// This function sorts an array of objects that include this code, alphabetically
// That's nothing new, except this sorts strings starting with a number correctly!

  # ---
  # Sorting
  # ---
def <=>(other)
    regex = /^[\d]+/
    if self.title =~ regex
      our_num = Regexp.last_match[0].to_i
      if other.title =~ regex
        other_num = Regexp.last_match[0].to_i
        return our_num <=> other_num
      end
    end
    self.title <=> other.title
  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS