0

i have an array with numbers like this:

list1 = [50, 2, 99, 1, 958, 9, 6, 80]

and i need to compare all elements: digit by digit. for exemple, it will be important to compare "9", "99" and "958", because i'll need to put "99" at the end like this:

list1 = [1, 2, 50, 6, 80, 9, 958, 99]

Does Ruby has a function for this? thank you very much for your help :)

3
  • 2
    Can you provide some more context on your sorting rules? You can use list1.map(&:to_s).sort to convert to strings and that sorts it like what I think you're asking, but your 2nd example doesn't have the first half of your list sorted (e.g. why does 50 sort before 2?) Commented Apr 15, 2019 at 17:52
  • Hey! i have to write the biggest number with all the elements of the array. for example : [50, 2, 1, 9] should output the number "95021". that's why i have to compare the elements digits by digits. (and sorry for my 2nd example, i didn't check the beginning of the array ;) ) Commented Apr 15, 2019 at 18:43
  • 1
    "Sorry for my 2nd example"? Are you referring to the example in your question? If so, why haven't you edited the question to fix it? I see you are new to SO, but isn't it common sense that you should correct errors in your question? Also, if you want to construct a string (e.g, "95021"), why don't you say that in your question? Commented Apr 15, 2019 at 22:44

3 Answers 3

3

I think what you are looking for is this:

list1.sort_by(&:to_s)

So, this will lead to:

[50, 2, 99, 1, 958, 9, 6, 80].sort_by(&:to_s)
#=> [1, 2, 50, 6, 80, 9, 958, 99]

[50, 2, 1, 9].sort_by(&:to_s)
#=> [1, 2, 50, 9]

If you want reverse order, you can do:

[50, 2, 99, 1, 958, 9, 6, 80].sort_by(&:to_s).reverse
#=> [99, 958, 9, 80, 6, 50, 2, 1] 

[50, 2, 1, 9].sort_by(&:to_s).reverse
#=> [9, 50, 2, 1] 

And you can do .join to get the joined string:

[50, 2, 1, 9].sort_by(&:to_s).reverse.join
#=> "95021"
Sign up to request clarification or add additional context in comments.

Comments

2

Try this: compare values converted to string, using String#<=> with Array#sort:

list1.sort { |a, b| a.to_s <=> b.to_s }
#=> [1, 2, 50, 6, 80, 9, 958, 99]

If I understand the question, even if the first three element of your output are confusing me about the logic.

1 Comment

While this seemed correct, and elegant at first glance, this doesn't actually work. The <=> comparator assumes that the longer string is greater than the shorter string (when leading characters are the same). However, in this case, the shorter string should be considered greater. [10, 9, 95] is already sorted as per this logic, but 95910 is less than 99510.
-1

To extract just the first digit, you just need to map twice:

list = [50, 2, 99, 1, 958, 9, 6, 80]

p list.map(&:to_s).map { |n| n[0] }
# => ["5", "2", "9", "1", "9", "9", "6", "8"]

Update: The solution I've linked below is incorrect. The problem isn't as simple as it seemed.

Old answer follows:

However, the problem itself looked interesting, so I gave it a shot - i.e, to form the largest possible number from a list of numbers. However, I don't want to spoil the fun for you, so here's a link to a gist that solves the problem through normalization, in case you're interested.

2 Comments

many thanks for your help Jay Dorsey & Hari Gopal! i will try the "double mapping" tomorrow and of course i'll check your solution, it's always interesting to read others methods ;)
i tried it and this is perfect!!!! i was thinking about converting it to string but i wasn't sure about how to proceed after that! thank you everyone for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.