# How do I test if variable Name starts with Alphabet or not?

**URL:** <https://www.ruby-forum.com/t/how-do-i-test-if-variable-name-starts-with-alphabet-or-not/143687>\
**Category:** Rails\
**Created:** [July 21, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/how-do-i-test-if-variable-name-starts-with-alphabet-or-not/143687 "2008-07-21T21:02:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [July 21, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/how-do-i-test-if-variable-name-starts-with-alphabet-or-not/143687/1 "2008-07-21T21:02:51Z")

</div>

Hi,

I need to check if a variable “name” starts with a variable or not, how  
do i do that?

Essential, i want to sort array of names alphabetically : i was using  
names.sort\_by{|c| "c[‘name’]}

But if name has numerical or special characters, they score well in  
sorting and are there in the beginning of the sorted list. But I want  
those special names to be in the end. How do i do this? Please suggest

regards,  
SG

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [July 21, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/how-do-i-test-if-variable-name-starts-with-alphabet-or-not/143687/2 "2008-07-21T21:55:24Z")

</div>

If I understand correctly, you have letters and numbers in your array?  
Something like:

[“a”, “b”, 23, 1]

I’m just a beginner in Ruby and I know it’s not the best way to do it,  
but here’s what I would do. I would separate the array into two  
different arrays (e.g., numbers and strings), sort them separately and  
then concatenate the arrays.

numbers=[]  
strings=[]  
[“a”, “b”, 23, 1].each { |x| if x.is\_a?(String) then strings \<\< x else  
numbers \<\< x end }  
result = strings.sort + numbers.sort

Something like that? If your array members are strings with numbers  
embedded in them, then use regular expressions to separate them into  
the two arrays. Hope I’m not way off the mark.

Regards,  
S
