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

  • Home
  • SEARCH
  • 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 7758413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:21:13+00:00 2026-06-01T13:21:13+00:00

Given an array like x = [1, 3, 5, -1, -3, -5] If we

  • 0

Given an array like

x = [1, 3, 5, -1, -3, -5]

If we use the command

x.sort {|i| i}

We are given

x = [-1, -3, -5, 1, 3, 5]

Is there any way, given our array, to have it return it in proper ascending/descending order with negatives? E.g.

x = [-5, -3, -1, 1, 3, 5] or [5, 3, 1, -1, -3, -5]

EDIT:

It seems like x.sort would solve this problem, but if there was a more sophisticated problem in which I want to sort from my array based on values given in a hash e.g.

x = [{:i=>1}, {:i=>2}, {:i=>3}, {:i=>4}, {:i=>5}]
y = {3=>10, 4=>-1, 2=>-2, 5=>-3, 1=>-4}

I want to be able to sort x based on the values in y so that my result is

x = [{:i=>3}, {:i=>4}, {:i=>2}, {:i=>5}, {:i=>1}]
  • 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-01T13:21:14+00:00Added an answer on June 1, 2026 at 1:21 pm
    x = [1, 3, 5, -1, -3, -5]
    x.sort # => [-5, -3, -1, 1, 3, 5]
    x.sort {|a,b| a <=> b} # => [-5, -3, -1, 1, 3, 5]
    x.sort {|a,b| b <=> a} # => [5, 3, 1, -1, -3, -5]
    

    Your example yields unexpected results because of the return value expected by the Array#sort method. Basically, when you return just the first argument (when two are expected), the interpreter only looks at the sign (-/0/+) of the element and uses that for ordering. So depending on the underlying sort algorithm, when it yields pairs from the array to your block it’s only looking at the sign of the first element, so something like:

    compare(1, 3) # => 1 (wrong, should be -1 since 1 < 3)
    compare(1, 5) # => 1 (wrong, should be -1 since 1 < 5)
    compare(1, -1) # => 1 (right, by complete accident)
    

    [Edit] Per your updated question, try using the following sort comparator block:

    x.sort! {|a,b| y[b[:i]] <=> y[a[:i]]}
    x # => [{:i=>3}, {:i=>4}, {:i=>2}, {:i=>5}, {:i=>1}]
    

    Which reads – sort the array x in place by comparing each element pair, a and b, by looking up their :i attribute in the hash y and comparing those values in descending order.

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

Sidebar

Related Questions

Given an array 'a' I would like to sort the array by columns sort(a,
I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
I would like to sort an array of strings in a directory, given a
Given an array like {one two, three four five}, how'd you calculate the total
Given an array like this: Array => ( [0] => 1, [1] => 2,
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
How do I create an array in smarty from a given string like 22||33||50
I have an array outside: $myArr = array(); I would like to give my
For example, I have an array like this: array(4) ( a => string(0) b
I'd like to use a std::vector to control a given piece of memory. First

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.