# Regarding Arrays

**URL:** <https://www.ruby-forum.com/t/regarding-arrays/189191>\
**Category:** Ruby\
**Created:** [May 26, 2010, 1:25pm UTC](https://www.ruby-forum.com/t/regarding-arrays/189191 "2010-05-26T13:25:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![souravhaldar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@souravhaldar](https://www.ruby-forum.com/u/souravhaldar)\
**Post date:** [May 26, 2010, 1:25pm UTC](https://www.ruby-forum.com/t/regarding-arrays/189191/1 "2010-05-26T13:25:24Z")

</div>

I am trying to build a Class ‘Employee’ in which age,address,idno  
methods should be there . So i need to built a Array in which the  
elements are called by idno and get sorted.

---

<div class="post-metadata">

**Author:** ![souravhaldar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@souravhaldar](https://www.ruby-forum.com/u/souravhaldar)\
**Post date:** [May 26, 2010, 3:12pm UTC](https://www.ruby-forum.com/t/regarding-arrays/189191/2 "2010-05-26T15:12:09Z")

</div>

2010/5/26 Sourav H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am trying to build a Class ‘Employee’ in which age,address,idno  
> methods should be there . So i need to built a Array in which the  
> elements are called by idno and get sorted.

Not sure what you’re after, probably something like this

Employee = Struct.new :idno, …

employees = […]  
sorted = employees.sort\_by {|e| e.idno}  
sorted = employees.sort\_by(&:idno) # alternative

Cheers

robert

---

<div class="post-metadata">

**Author:** ![souravhaldar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@souravhaldar](https://www.ruby-forum.com/u/souravhaldar)\
**Post date:** [May 27, 2010, 11:00am UTC](https://www.ruby-forum.com/t/regarding-arrays/189191/3 "2010-05-27T11:00:38Z")

</div>

Robert K. wrote:

> 2010/5/26 Sourav H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > I am trying to build a Class ‘Employee’ in which age,address,idno  
> > methods should be there . So i need to built a Array in which the  
> > elements are called by idno and get sorted.
> 
> Not sure what you’re after, probably something like this
> 
> Employee = Struct.new :idno, …
> 
> employees = […]  
> sorted = employees.sort\_by {|e| e.idno}  
> sorted = employees.sort\_by(&:idno) # alternative
> 
> Cheers
> 
> robert  
> Thanx robert

I have built a program but wants to make it more short . SO plz see  
wether is it possible or not

class Employee  
attr\_accessor :name ,:age, :address, :idno  
end

a = Array.new  
e1 = Employee.new  
e1.name = “Cindy”  
e1.age = 24  
e1.address = “Mumbai”  
e1.idno = 121  
a.push(e1) # insert the values of e1 in the array a  
puts ‘------------------------’  
e2 = Employee.new  
e2.name = “Kennedy”  
e2.age = 29  
e2.address = “Kolkata”  
e2.idno = 131  
a.push(e2) # insert the values of e2 in the array a  
puts ‘------------------------’  
e3 = Employee.new  
e3.name = “Pearl”  
e3.age = 35  
e3.address = “Chennai”  
e3.idno = 141  
a.push(e3) # insert the values of e3 in the array a

a.sort!{|x,y|  
x.idno \<=\> y.idno

}

a.collect!{|x|puts “#{x.name} #{x.age} #{x.address} #{x.idno}”}

---

<div class="post-metadata">

**Author:** ![souravhaldar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@souravhaldar](https://www.ruby-forum.com/u/souravhaldar)\
**Post date:** [May 27, 2010, 11:48am UTC](https://www.ruby-forum.com/t/regarding-arrays/189191/4 "2010-05-27T11:48:30Z")

</div>

2010/5/27 Sourav H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > employees = […]  
> > sorted = employees.sort\_by {|e| e.idno}  
> > sorted = employees.sort\_by(&:idno) # alternative

> I have built a program but wants to make it more short . SO plz see  
> wether is it possible or not

It is.

Employee = Struct.new :name, :age, :address, :idno

a = [  
Employee[“Cindy”, 24, “Mumbay”, 121],  
Employee[“Kennedy”, 29, “ruby-talk”, 131],  
]

a.sort\_by(&:idno).each do |x|  
puts x.map(&:to\_s).join(" ")  
end

🙂

> a.push(e1) # insert the values of e1 in the array a  
> puts ‘------------------------’  
> e2 = Employee.new

You do not need a new local variable here: you can reuse “e1” since  
the object created above is in the Array already.

> e3.idno = 141  
> a.push(e3) # insert the values of e3 in the array a
> 
> a.sort!{|x,y|  
> x.idno \<=\> y.idno
> 
> }
> 
> a.collect!{|x|puts “#{x.name} #{x.age} #{x.address} #{x.idno}”}

You should use #each here instead of #collect! because I believe you  
only want to output all elements in the Array but not change the Array  
itself.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![souravhaldar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@souravhaldar](https://www.ruby-forum.com/u/souravhaldar)\
**Post date:** [May 27, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/regarding-arrays/189191/5 "2010-05-27T13:19:15Z")

</div>

Thanx Robert

One more Question !

How we implement DoublyLinkedList i.e append(node)  
,search(node),delete(node) in Ruby ?

---

<div class="post-metadata">

**Author:** ![souravhaldar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@souravhaldar](https://www.ruby-forum.com/u/souravhaldar)\
**Post date:** [May 27, 2010, 1:41pm UTC](https://www.ruby-forum.com/t/regarding-arrays/189191/6 "2010-05-27T13:41:19Z")

</div>

Sourav H. wrote:

> Thanx Robert
> 
> One more Question !
> 
> How we implement DoublyLinkedList i.e append(node)  
> ,search(node),delete(node) in Ruby ?

And can you tell me that how to build a good concept on Ruby.
