Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How would you sort list in Python and array in Ruby like:

["car", "z", "9", "bus", "3"] 

to have this array in return:

["bus", "car", "3", "z", "9"]

I've started building it in Ruby, because I know it better. I tried .sort with no parameters. Then I started writing Insertion sort with hopes I will rebuild it to my order and wrote this method.

def sort
 @array.each do |value|
   index = @array.index(value)
   i = index - 1
   while i >= 0
     if value < @array[i]
       @array[i + 1] = @array[i]
       @array[i] = value
     elsif (value =~ /\d/) == 0
       # I wanted to do something here, whenever it stops at number to word comparison   but didn't come up with anything workable
     else
       break
     end
   end
 end

end

All I can get is ["3", "9", "bus", "car", "z"] But that's a code challenge I have to complete and the goal is to sort the array of strings in alphabetical and numerical order keeping keeping numeric strings indexes as in original array, just putting them in ascending order. I was thinking to create 2 hashes for numbers with and words having their keys as indexes in original array and sort just the values, and then inject them in correct order in a new array, but wasn't able to write the code for it and still not sure if that would be the best idea.

share|improve this question
5  
First, pick one language. Second, explain why you think that should be the output. Third, show what you have tried so far and describe what exactly the problem with it is. –  jonrsharpe Nov 5 '14 at 11:52
5  
And most of all, explain why you think "3" should be sorted before "z", but "9" comes after. I see no logical progression here. –  Martijn Pieters Nov 5 '14 at 11:54
    
Sorry for poor start, it was early morning for me. Just edited it. –  Dmitry J Nov 5 '14 at 14:53
    
I'm voting to reopen because it is clear now, but have a question: if it is a code challenge, isn't the point to solve it yourself instead of asking StackOverflow to do it? (PS your idea is valid, just break it down into discrete steps and you can solve it) –  Mark Thomas Nov 5 '14 at 15:12
1  
Hint: 1. Store indices of numeric elements. 2. partition the array. 3. Sort each partition. 4. insert numeric elements back in. –  Mark Thomas Nov 5 '14 at 15:17

2 Answers 2

So here is how I've solved it. Thanks to Mark Thomas for hint with partition and insert methods. Please comment on your thoughts and suggestions about efficiency and clarity of code.

def sort
  # storing indicies of number strings to map them back later
  num_indicies = @array.map {|i| @array.index(i) if (i =~ /\d/) == 0}.compact
  # if there are no numbers
  if num_indicies.empty?
    @array.sort!
    #if there are only numbers
  elsif num_indicies.length == @array.length
    @array = @array.map {|n| n.to_i}.sort
  else
    # separating numbers and words for proper sort
    separation = @array.partition {|c| (c =~ /\d/) == 0}
    # sorting first array. Converting to integer in order to sort numbers bigger than 10
    separation[0] = separation[0].map {|n| n.to_i}.sort
    # sorting array of words and letters
    separation[1].sort!
    # inserting numbers in their original spots
    index = 0
    separation[0].each do |num|
      #inserting sorted integers inside of array of sorted strings, simultaniously converting them into strings
      @array = separation[1].insert(num_indicies[index], num.to_s)
      # switching index for another iteration
      index += 1
    end
  end
end
share|improve this answer

You need to find a gem (or write something yourself) that can turn the numbers into their word form.

Like, for instance:

https://github.com/radar/humanize

The rest should be obvious.

share|improve this answer
    
Well, the will mix it up a bit. 293 is two hundred ninty three, which is ["two", "hundred", "ninty", "three"].sort == ["hundred", "ninty", "three", "two"] And that's not the result I need. –  Dmitry J Nov 6 '14 at 17:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.