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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:37:59+00:00 2026-05-26T03:37:59+00:00

I have an object with two doubles: class SurveyData(){ double md; double tvd; }

  • 0

I have an object with two doubles:

class SurveyData(){
double md;
double tvd;
}

I have a list of these values that is already sorted ascending. I would like to find and return the index of the object in the list with the maximum tvd value that is less than or equal to a double. How can I efficiently accomplish this task?

  • 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-26T03:38:00+00:00Added an answer on May 26, 2026 at 3:38 am

    Assuming you’ve got LINQ and are happy to use TakeUntil from MoreLINQ, I suspect you want:

    var maxCappedValue = values.TakeUntil(data => data.Tvd >= limit)
                               .LastOrDefault();
    

    That will get you the first actual value rather than the index, but you could always do:

    var maxCappedPair = values.Select((value, index) => new { value, index })
                               .TakeUntil(pair => pair.value.Tvd >= limit)
                               .LastOrDefault();
    

    for the index/value pair. In both cases the result would be null if all values were above the limit.

    Of course, it would be more efficient to use a binary search – but also slightly more complicated. You could create a “dummy” value with the limit TVD, then use List<T>.BinarySearch(dummy, comparer) where comparer would be an implementation of IComparer<SurveyData> which compared by TVD. You’d then need to check whether the return value was non-negative (exact match found) or negative (exact match not found, return value is complement of where it would be inserted).

    The difference in complexity is between O(n) for the simple scan, or O(log n) for the binary search. Without knowing how big your list is (or how important performance is), it’s hard to advise whether the extra implementation complexity of the binary search would be worth it.

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

Sidebar

Related Questions

I have two complex (i.e. objects with string, int, double, List and other home
I have two tables: object that has object_id column and avalues that have object_id
I have two interfaces IHeaderRow , and IDetailRow I then have an object that
I have two Collection objects, I want to associate each object of these two
I have an object that I'm serializing that contains a lot of doubles and
I have simple class Point with two fields of type double . I asked
I have a module that looks something like this: def __myFunc(): ... class MyClass(object):
I have two object : Mother and Child. Mother have many Children, How can
Lets say I have an object with two boolean properties: public bool AdvancedMode{ get;
I have a Person object with two constructors - one takes an int (personId),

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.