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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:25:55+00:00 2026-05-26T20:25:55+00:00

I know how to put an array in order, but in this case I

  • 0

I know how to put an array in order, but in this case I just want to see if it is in order. An array of strings would be the easiest, I imagine, and answers on that front are appreciated, but an answer that includes the ability to check for order based on some arbitrary parameter is optimal.

Here’s an example dataset. The name of:

[["a", 3],["b",53],["c",2]]

Where the elements are themselves arrays containing several elements, the first of which is a string. I want to see if the elements are in alphabetical order based on this string.

  • 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-26T20:25:55+00:00Added an answer on May 26, 2026 at 8:25 pm

    It looks like a generic abstraction, let’s open Enumerable:

    module Enumerable
      def sorted?
        each_cons(2).all? { |a, b| (a <=> b) <= 0 }
      end
    end
    
    [["a", 3], ["b", 53],["c", 2]].sorted? #=> true
    

    Notice that we have to write (a <=> b) <= 0 instead of a <= b because there are classes that support <=> but not the comparator operators (i.e. Array), since they do not include the module Comparable.

    You also said you’d like to have the ability "to check for order based on some arbitrary parameter":

    module Enumerable  
      def sorted_by?
        each_cons(2).all? { |a, b| ((yield a) <=> (yield b)) <= 0 }    
      end
    end
    
    [["a", 3], ["b", 1], ["c", 2]].sorted_by? { |k, v| v } #=> false
    

    Using lazy enumerables (Ruby >= 2.1), we can reuse Enumerable#sorted?:

    module Enumerable  
      def sorted_by?(&block)
        lazy.map(&block).sorted?
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to put all of my ListView rows into
I dont't know how to put that, but I need y coordinates of the
I'm starting in the Rails world and i want to know how to put
I'd like to install some presentation templates, but don't know where to put them...
I know that I should put all the html elements in body tag, but
I know java and would normally put in getter/setter methods. I am interested in
I know it sounds weird but I am required to put a wrapping try
Hey guys I need to know who to put these array objects into two
just to know if someone have any idea why this bug is happening. I
I know I can put escaped HTML tags in string resources. However, looking at

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.