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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:52:27+00:00 2026-05-12T17:52:27+00:00

Of all objects matching a condition, find the one closest to a position. Supposedly

  • 0

Of all objects matching a condition, find the one closest to a position. Supposedly a very common problem. The current code looks like this:

protected Foo FindClosestFooMatching (Vec pos, Func<Foo, bool> matches)
{
    float bestDist = float.MaxValue;
    Foo bestFoo = null;

    foreach (Foo foo in AllFoos()) {
        if (matches (foo)) {
            float dist = pos.Dist (foo.Center);
            if (dist <= bestDist) {
                bestDist = dist;
                bestFoo = foo;
            }
        }
    }
    return bestFoo;
}

I’m thinking of several ways to refactor this code, but failed to find a really nice one yet. If you’ve got a minute, give it a shot. 🙂

Edit: Regarding Eric’s questions. It’s a standard 3D space with Euclidian metric (= fast). Point clustering and junk query likelihood is unknown.

  • 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-12T17:52:27+00:00Added an answer on May 12, 2026 at 5:52 pm

    Can’t compile right now, but I hope this does the job.

    var foos = AllFoos().Where (x => matches (x));
    return foos.OrderBy (x => pos.Dist (x.Center)).FirstOrDefault ();
    

    The important point to notice here is that every operation, including OrderBy, is subject to deferred execution, so the performance should be ok.

    This would be a nicer looking solution:

    var res = from foo in AllFoos()
        where matches (foo)
        orderby pos.Dist (foo.Center)
        select foo;
    return res.FirstOrDefault ();
    

    Edit: I found this question to be a valuable source in this issue. Since OrderBy actually orders the whole list first, it’s of course slow (as Eric said, O(n log n). A better solution would be to use Aggregate or Jon Skeet’s propsed MinBy extension (see above link for examples).

    I actually love the MoreLinq solution, as it reduces the code to:

    return World.AllNodes
        .Where (x => matches (x.T))
        .MinBy (x => pos.DistSq (x.T.Volume.Center));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i write the following code Post.find(:all,:group=>'comments') It only gives me the first record
All objects in my program inherit from a Container class. The Container class has
All objects used as keys in NS(Mutable)Dictionaries must support the NSCopying protocol, and those
I want to retrieve all objects (not DOM elements) of a given type created
Is there a way to look through the cache for all objects in the
I would like to get a reference to all objects in the Java heap,
Is it possible to create a criteria that lists all objects sorted by sope
when we bind to heterogeneous collection of objects, not all objects have the same
I need to write some sql that will allow me to query all objects
It's my understanding that in Spring, all objects are treated by default as singletons.

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.