This page last changed on Jan 26, 2009 by skim.

Ruby Tips

Sorting Names

class User
  def initialize(name)
    @name = name
  def name

# see: Random Name Generator:
# note the last three names I added ...

ulist = 
  ["Roderic  Joe Kimmons", "Isolda  Wells", "A. J.  Knight", "Shana  Hawkins", 
  "Millicent  Putnam", "Arlene  Swift", "Lalage  Rohtin", "Lashawn  Tomey", 
  "Mitch  Wyatt", "Serena  Warner", "Sylvester  Carpenter", "Dudley  Turzanski", 
  "Brent  Mcelroy", "Luvinia  Werry", "Ruth  Alice Elisabeth Hook", "Trixie  Davis", 
  "Dane  Hallauer", "Valerie  Lane", "Precious  Hiles", "Sherry  Sanner", 
  "Prince", "", nil]

# create an array of User objects

@users = []
ulist.each { |u| @users << }

# a complicated way to sort the names

def sort_users
  @users.sort do |u1, u2| 
    sep = /[\s,]+/
    if and not
      n1 =
      n1 = [" "," "]

    if and not
      n2 =   
      n2 = [" "," "]

    last1 = n1[n1.length-1]
    last2 = n2[n2.length-1]
    first1 = n1.length > 1 ? n1[0] : ''
    first2 = n2.length > 1 ? n2[0] : ''

    if last1 < last2 
    elsif last1 > last2
    elsif first1 < first2
    elsif first1 > first2

@sorted_users1 = sort_users

# a simpler way to sort the users

@sorted_users2 = @users.sort_by {|user| ( && ! ?, 0) : [""] }

# or spread out onto 8 lines

# @sorted_users2 = @users.sort_by { |user| 
#   if && !
#, 0)
#   else
#     [""]
#   end
# }

puts sprintf("%-40s %-40s %-40s", "unsorted names", "complicated sort", "simpler sort")
puts '-' * 120
@users.each_index { |i| puts sprintf("%-40s %-40s %-40s", @users[i].name, @sorted_users1[i].name, @sorted_users2[i].name) }

The result:

Unable to find source-code formatter for language: shell. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
$ ruby ~/Desktop/sorting_users.rb 

unsorted names                           complicated sort                         simpler sort                            
Roderic  Joe Kimmons                                                                                                      
Isolda  Wells                                                                                                             
A. J.  Knight                            Sylvester  Carpenter                     Sylvester  Carpenter                    
Shana  Hawkins                           Trixie  Davis                            Trixie  Davis                           
Millicent  Putnam                        Dane  Hallauer                           Dane  Hallauer                          
Arlene  Swift                            Shana  Hawkins                           Shana  Hawkins                          
Lalage  Rohtin                           Precious  Hiles                          Precious  Hiles                         
Lashawn  Tomey                           Ruth  Alice Elisabeth Hook               Ruth  Alice Elisabeth Hook              
Mitch  Wyatt                             Roderic  Joe Kimmons                     Roderic  Joe Kimmons                    
Serena  Warner                           A. J.  Knight                            A. J.  Knight                           
Sylvester  Carpenter                     Valerie  Lane                            Valerie  Lane                           
Dudley  Turzanski                        Brent  Mcelroy                           Brent  Mcelroy                          
Brent  Mcelroy                           Prince                                   Prince                                  
Luvinia  Werry                           Millicent  Putnam                        Millicent  Putnam                       
Ruth  Alice Elisabeth Hook               Lalage  Rohtin                           Lalage  Rohtin                          
Trixie  Davis                            Sherry  Sanner                           Sherry  Sanner                          
Dane  Hallauer                           Arlene  Swift                            Arlene  Swift                           
Valerie  Lane                            Lashawn  Tomey                           Lashawn  Tomey                          
Precious  Hiles                          Dudley  Turzanski                        Dudley  Turzanski                       
Sherry  Sanner                           Serena  Warner                           Serena  Warner                          
Prince                                   Isolda  Wells                            Isolda  Wells                           
                                         Luvinia  Werry                           Luvinia  Werry                          
                                         Mitch  Wyatt                             Mitch  Wyatt                        


Document generated by Confluence on Jan 27, 2014 16:56