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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:34:40+00:00 2026-05-19T01:34:40+00:00

Class A has the following comparator: class A attr_accessor x def my_comparator(a) x**2 <=>

  • 0

Class A has the following comparator:

class A
  attr_accessor x

  def my_comparator(a)
    x**2 <=> (a.x)**2
  end
end

I would like to use this comparator to sort an array where each item is of class A:

class B
  def my_method
    items.sort!(<how can I pass my_comparator here ?>)
  end
end

How should I pass my_comparator to sort!?

  • 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-19T01:34:40+00:00Added an answer on May 19, 2026 at 1:34 am

    Define your own <=>, and include Comparable. This is from the Comparable doc:

    class SizeMatters
      include Comparable
      attr :str
      def <=>(an_other)
        str.size <=> an_other.str.size
      end
      def initialize(str)
        @str = str
      end
      def inspect
        @str
      end
    end
    
    s1 = SizeMatters.new("Z")
    s2 = SizeMatters.new("YY")
    s3 = SizeMatters.new("XXX")
    s4 = SizeMatters.new("WWWW")
    s5 = SizeMatters.new("VVVVV")
    
    s1 < s2                       #=> true
    s4.between?(s1, s3)           #=> false
    s4.between?(s3, s5)           #=> true
    [ s3, s2, s5, s4, s1 ].sort   #=> [Z, YY, XXX, WWWW, VVVVV]
    

    You don’t actually have to include Comparable, but you get extra functionality for free if you do that after having defined <=>.

    Otherwise, you can use Enumerable’s sort with a block if your objects implement <=> already.

    Another way to use several different comparisons is to use lambdas. This uses the new 1.9.2 declaration syntax:

    ascending_sort  = ->(a,b) { a <=> b }
    descending_sort = ->(a,b) { b <=> a }
    
    [1, 3, 2, 4].sort( & ascending_sort ) # => [1, 2, 3, 4]
    [1, 3, 2, 4].sort( & descending_sort ) # => [4, 3, 2, 1]
    
    foo = ascending_sort
    [1, 3, 2, 4].sort( & foo ) # => [1, 2, 3, 4]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
Write a class ListNode which has the following properties: int value; ListNode *next; Provide
I am wrapping a native C++ class, which has the following methods: class Native
I'm trying to write a test for the following method: public class Sort {
I has the following class that represents a View that is touchable and draw
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
A class has a property (and instance var) of type NSMutableArray with synthesized accessors
Some class has ugly field called URL, but Id rather call it file. public
FxCop has the CollectionPropertiesShouldBeReadOnly rule that complains if your class has some kind of

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.