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

The Archive Base Latest Questions

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

Is there a better way to find closest object with lesser marker? class Prop

  • 0

Is there a better way to find closest object with lesser marker?

class Prop implements Comparable {
  BigDecimal marker
  String value

  int compareTo(other) {
    return marker <=> other.marker
  }

}

def props = [new Prop(marker: 0G, value: "1st"),
new Prop(marker: 20G, value: "2nd"),
new Prop(marker: 30G, value: "3rd"),
new Prop(marker: 40G, value: "4th"),
new Prop(marker: 50G, value: "5th"),
new Prop(marker: 100G, value: "6th"),
new Prop(marker: 1000G, value: "7th")]

def whereAmI = 44G

def here = props.findAll{it.marker <= whereAmI}.max()
  • 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-25T20:14:34+00:00Added an answer on May 25, 2026 at 8:14 pm

    Edit: Updated to return the correct object type, and nulls.

    Assuming that order isn’t guaranteed, you could use the inject method:

    // Inject null, so if nothing is lower than max, this returns null
    def here = props.inject(null){ max, it ->
        (it.marker <= whereAmI && (!max || it > max)) ? it : max
    }
    

    If the list is always sorted, then simply use this:

    // Inject null, so if nothing is lower than max, this returns null
    def here = props.inject(null){ max, it ->
        (it.marker <= whereAmI) ? it : max
    }
    

    The benefit here is you only loop over the set once, and you don’t create an additional interim List of lesser values.

    Now, depending on your list size, the argument may be that your original example is a lot easier to read, and a lot clearer. Clarity in code can trump performance.

    (Note: inject is Groovy’s version of reduce or fold.)

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

Sidebar

Related Questions

Is there a better way to find all form elements in a Drupal form
Is there a better/simpler way to find the number of images in a directory
Is there better way to delete a parameter from a query string in a
Is there a better way of binding a list of base class to a
Is there a better way to find which X gives me the Y I
Is there a better way to find if the current element of a collection
I'd like to know what is there a better way to find the first
Is there a better way to flash a window in Java than this: public
Is there a better way to forcefully disconnect all users from an Oracle 10g
Is there a better way to do this? -(NSDate *)getMidnightTommorow { NSCalendarDate *now =

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.