# Randomly Sorting Strings

**URL:** <https://www.ruby-forum.com/t/randomly-sorting-strings/85200>\
**Category:** Ruby\
**Created:** [February 11, 2007, 8:25am UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200 "2007-02-11T08:25:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 8:25am UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/1 "2007-02-11T08:25:37Z")

</div>

Hi! I’m trying to write a program that will print out a different  
string each time it’s run from a list of strings.

For example:

example = [‘one’, ‘two’, ‘three’]

# sort them somehow

# ‘two’ prints out as the output, for example

# run it again and a different string prints out

Make sense? I’ve tried srand & rand but can’t do this with strings. Do  
I need to forget the arrays and try something else? I’ve looked at the  
Pickaxe book and Ruby Cookbook but can’t find anything. Thanks!

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 8:47am UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/2 "2007-02-11T08:47:38Z")

</div>

try

example= %w[one two three cat dog cow table]  
puts example[rand(example.size)]

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 9:09am UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/3 "2007-02-11T09:09:11Z")

</div>

Sharon P. wrote:

> try
> 
> example= %w[one two three cat dog cow table]  
> puts example[rand(example.size)]
> 
> Cheers,  
> Dave

Another example would be:

example= %w[one two three cat dog cow table].sort\_by { rand }  
example.each { |string|  
puts string  
}

that will print all in random order

my regards

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/4 "2007-02-11T12:05:05Z")

</div>

On 2/11/07, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Another example would be:
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> This is not really what you want to do but I cannot post more for now,  
> this is a Ruby Q. Spoiler!!!  
> Robert

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/5 "2007-02-11T13:58:14Z")

</div>

Hi –

On Sun, 11 Feb 2007, Stefan R. wrote:

> example= %w[one two three cat dog cow table].sort\_by { rand }  
> example.each { |string|  
> puts string  
> }

You can replace the last three lines with:

puts example

🙂

David

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/6 "2007-02-11T14:00:14Z")

</div>

On 2/11/07, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I apologize. I didn’t read the current quiz.  
> Does that mean I have to check back with the current quiz when somebody  
> asks for help in here?
> 
> my regards
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

No I do not think so, no need to apologize either 😉

Well James is the “boss” of the quiz, but the ML and the Quiz are two  
distinct matters.  
I personally chose to try to avoid to post any material too much related  
to  
the Quiz and furthermore I point it out to others who might not be  
interested in the Quiz.

Quizzes can be spoiled by posts of course, I redrew a quiz once because  
the  
issue was discussed three days before its due date, actually I think it  
was  
a good thing because after the discussion I did not think my Quiz was  
very  
brilliant any more:). James was just great he had another quiz to come  
up  
with immediately.

AFAIAC your reply has been very thoughtful, do not worry!

Cheers  
Robert

P.S. Quiz Advocacy, checking the quiz is a good idea anyway :)))

R.

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/7 "2007-02-11T16:27:24Z")

</div>

On 2/11/07, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > that will print all in random order
> > 
> > my regards
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > This is not really what you want to do but I cannot post more for now,  
> > this is a Ruby Q. Spoiler!!!  
> > Robert

Ok time to clarify.  
#sort\_by{ rand } is fine, it is my head that is broken, so it is time  
for  
me to apologize  
#sort{ rand } of course is not, but I’ll get new reading glasses soon.

Robert

–

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/8 "2007-02-11T20:54:44Z")

</div>

On Feb 11, 2007, at 6:59 AM, Robert D. wrote:

> I personally chose to try to avoid to post any material too much  
> related to  
> the Quiz and furthermore I point it out to others who might not be  
> interested in the Quiz.

Overlap happens. No big deal. It just means I chose well for the  
quiz topic and it will be helpful in the future. 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 11, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/randomly-sorting-strings/85200/9 "2007-02-11T12:15:11Z")

</div>

Robert D. wrote:

> On 2/11/07, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## Another example would be:
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > This is not really what you want to do but I cannot post more for now,  
> > this is a Ruby Q. Spoiler!!!  
> > Robert

I apologize. I didn’t read the current quiz.  
Does that mean I have to check back with the current quiz when somebody  
asks for help in here?

my regards
