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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:55:42+00:00 2026-06-12T19:55:42+00:00

Is there a way to evaluate the order of an array? I want to

  • 0

Is there a way to evaluate the order of an array? I want to find an array of records ordered by created_at date and then see if the numbers in that array are in sequence?

For instance:

  Model.all.order(&:created_at).select("lesson_number)
  [1, 2, 4, 3, 5]

should fail because the numbers are not in sequence

I could execute two finds. One that is ordered by “lesson_number” and one that is ordered by created_at date. Convert them both the a string then compare the two. But, seems like a lot of work if a rails method exists to handle such a thing.

  • 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-12T19:55:43+00:00Added an answer on June 12, 2026 at 7:55 pm

    You can combine a couple methods in Ruby to do this pretty efficiently.

    input.each_cons(2).reduce(true) { |result, (a, b)| result && (a <=> b) < 0 }
    

    each_cons will iterate through your array yielding, in this case, each 2 consecutive items. Because we don’t pass it a block, it returns an enumerator that we can iterate through and get a single resulting value using reduce (a.k.a. inject).

    Our block compares a and b using <=> which will return -1, 0, or 1 depending on whether the first value is “less-than”, equal, or “greater than”. In this case, we want to make sure a is -1.

    In case you’re not familiar with it, the parenthesis in the block argument are Ruby 1.9+, and they allow the arguments to be splatted in (otherwise we would get a 2-item array in our block).

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

Sidebar

Related Questions

Is there a way to evaluate an enum? I have an enum that is
Is there a way to evaluate the minimum requirements of a software? I mean,
Is there a way to to evaluate a boolean expression and assign its value
Is there a way to convince the C preprocessor to evaluate a transcendental function
Is there way to automatically maximize the output window on hitting build and then
Is there a way to evaluate a global resource using WinJS directly in my
Is there a way to evaluate a JavaScript function on a webpage in QTP,
Is there a way to change the order django evaluates a template filter ?
I was wondering if there is a way to define hierarchy (not just order
Is there way to get file from windows xp command prompt? I tried to

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.