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 722823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:02:05+00:00 2026-05-14T06:02:05+00:00

I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence

  • 0

I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence how to use it?

An example from Ruby documentation:

   a = [ "d", "a", "e", "c", "b" ]
   a.sort                     #=> ["a", "b", "c", "d", "e"]
   a.sort { |x,y| y <=> x }   #=> ["e", "d", "c", "b", "a"]
  • 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-05-14T06:02:06+00:00Added an answer on May 14, 2026 at 6:02 am

    In your example

    a.sort
    

    is equivalent to

    a.sort { |x, y| x <=> y }
    

    As you know, to sort an array, you need to be able to compare its elements (if you doubt that, just try to implement any sort algorithm without using any comparison, no <, >, <= or >=).

    The block you provide is really a function which will be called by the sort algorithm to compare two items. That is x and y will always be some elements of the input array chosen by the sort algorithm during its execution.

    The sort algorithm will assume that this comparison function/block will meet the requirements for method <=>:

    • return -1 if x < y
    • return 0 if x = y
    • return 1 if x > y

    Failure to provide an adequate comparison function/block will result in array whose order is undefined.

    You should now understand why

    a.sort { |x, y| x <=> y }
    

    and

    a.sort { |x, y| y <=> x }
    

    return the same array in opposite orders.


    To elaborate on what Tate Johnson added, if you implement the comparison function <=> on any of your classes, you gain the following

    1. You may include the module Comparable in your class which will automatically define for you the following methods: between?, ==, >=, <, <= and >.
    2. Instances of your class can now be sorted using the default (ie without argument) invocation to sort.

    Note that the <=> method is already provided wherever it makes sense in ruby’s standard library (Bignum, Array, File::Stat, Fixnum, String, Time, etc…).

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

Sidebar

Related Questions

I am having a problem understanding how polymorphism works when using generics. As an
I'm having problems understanding how latest.integration works. I have an example that is not
Possible Duplicate: Objective-C - When to use 'self' I am having problem understanding the
I am having problem understanding Priority Inversion Snippet from the article: Consider there is
I'm having a problem understanding how to use my core data generated NSManagedObject .
I'm having a simple problem understanding how to parse stdObject returns from simple queries
i am studying apple obj-c guide and i am having problem understanding class types,
i'm new to c++ and having a little problem understanding about c++'s casting. According
I'm quite new to OpenId and I'm having a bit of a problem understanding
I've been having some difficulty in understanding the source of a problem. Below is

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.