Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:18:16+00:00 2026-06-11T18:18:16+00:00

I have an exercise in my programming book asking me to sort an array

  • 0

I have an exercise in my programming book asking me to sort an array of strings in Ruby without using any of the built in sorting. What I have sorts the top 5 correctly but then just stops and I haven’t been able to figure out why. Here is what I have so far:

numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

def sortArray myArray
    recursiveSort myArray, []
end

def recursiveSort myArray, sortedArray
    trashArray = myArray
    myArray.each do |num|
        largest = num
        trashArray.each do |comp|
            if comp > largest
                largest = comp
            end
        end
        sortedArray.push(largest)
        trashArray.delete_at(trashArray.index(largest))
    end
    puts sortedArray
end

sortArray numbers
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:18:17+00:00Added an answer on June 11, 2026 at 6:18 pm

    Here’s an example of what’s happening. From my terminal:

    $ irb
    ruby-1.9.3-p125 :001 > a = [1,2]
     => [1, 2] 
    ruby-1.9.3-p125 :002 > b = a
     => [1, 2] 
    ruby-1.9.3-p125 :003 > b.delete_at(b.index(1))
     => 1 
    ruby-1.9.3-p125 :004 > b
     => [2] 
    ruby-1.9.3-p125 :005 > a
     => [2] 
    

    Your variables trashArray and myArray are references to the same object in memory, so you’re removing items from myArray on the line: trashArray.delete_at(trashArray.index(largest)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up this programming exercise. using System; using System.Collections.Generic; using System.Linq; using
I have been given an exercise to solve the zebra puzzle using a constraint
I'm trying out several exercises from a Java programming book. I have the code
I am learning Objective-C using Stephen Kochan's excellent book Programming in Objective-C 2.0. I
I have a function (exercise 10.11 in Thompson's The Craft of Functional Programming )
I have a Java programming lab-exercise in a course with students that I'd like
I have been trying to solve this Concurrent Programming exam exercise (in C#): Knowing
I have an Assembly final tomorrow. Any one have some ideas for a programming
I've run into some trouble in my programming book while completing an exercise to
I have taken Problem #12 from Project Euler as a programming exercise and to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.