# Sorting array of names

**URL:** <https://www.ruby-forum.com/t/sorting-array-of-names/179043>\
**Category:** Ruby\
**Created:** [November 16, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/sorting-array-of-names/179043 "2009-11-16T13:01:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 16, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/sorting-array-of-names/179043/1 "2009-11-16T13:01:16Z")

</div>

Hi all,  
I have a Ruby program that reads in a text file of first names and  
surnames and prints them out in alphabetical order. Now I’m trying to  
add the option: if a person has a middle name, to print that out too and  
if they don’t, to just print out their first name and surname.  
I’m having issues when allowing a person to have a middle name or not  
though. I think it is a problem with my to\_s method and when I create a  
new person. I have attached my ruby file, if anyone has any help or  
advice on how to start this please let me know.  
The text file of names is just like this, with the first entry having no  
middle name and the second one having a middle name: John,Doe  
Jane,Doe,Amy

---

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 16, 2009, 1:05pm UTC](https://www.ruby-forum.com/t/sorting-array-of-names/179043/2 "2009-11-16T13:05:37Z")

</div>

> I have attached my ruby file

I don’t think you did

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 16, 2009, 1:09pm UTC](https://www.ruby-forum.com/t/sorting-array-of-names/179043/3 "2009-11-16T13:09:01Z")

</div>

Paul S. wrote:

> > I have attached my ruby file
> 
> I don’t think you did
> 
> –  
> Paul S.  
> [http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)
> 
> removed\_email\_address@domain.invalid

Ah I’m sorry when trying initially it came up with application error, I  
didn’t realise it was to be attached again. I saved it as a txt file  
this time.

---

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 16, 2009, 1:19pm UTC](https://www.ruby-forum.com/t/sorting-array-of-names/179043/4 "2009-11-16T13:19:01Z")

</div>

Jane O’Maley wrote:

> Ah I’m sorry when trying initially it came up with application error, I  
> didn’t realise it was to be attached again. I saved it as a txt file  
> this time.

def initialize fname, lname, mname=nil  
@fname = fname  
@lname = lname  
@mname = mname  
end

# This makes the last name nil if it’s not defined when you create the

person.

def to\_s

# easy-to-read solution

temp = “#{@lname}, #{fname}”  
if !@mname.nil?  
temp += “, #{mname}”  
end  
return temp  
end

---

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 16, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/sorting-array-of-names/179043/5 "2009-11-16T13:26:39Z")

</div>

> def initialize fname, lname, mname=nil  
> @fname = fname  
> @lname = lname  
> @mname = mname  
> end
> 
> # This makes the last name nil if it’s not defined when you create the
> 
> person.
> 
> def to\_s
> 
> # easy-to-read solution
> 
> temp = “#{@lname}, #{fname}”  
> if !@mname.nil?  
> temp += “, #{mname}”  
> end  
> return temp  
> end

Thank you very much for your help. Having sorted this out I was able to  
find and fix some other problems I had with it too and can keep moving  
forward with it.
