Skip to content

Added an option to sort the result set by min distance#22

Closed
joshpangell wants to merge 1 commit intogilmoreorless:masterfrom
joshpangell:master
Closed

Added an option to sort the result set by min distance#22
joshpangell wants to merge 1 commit intogilmoreorless:masterfrom
joshpangell:master

Conversation

@joshpangell
Copy link

@gilmoreorless's wonderful plugin does a great job of finding the nearest elements. However, I was missing the ability to find the nearest elements sorted in order of proximity nearest to furthest. The current result set it in order of DOM tree. I had posted this as an issue here, but have gone ahead and made a fix for it instead.

This simple change adds the option sortMinDist to receive the result set ordered nearest to furthest. Here is a fiddle to see the update in action.

Usage:

$.nearest({
    x: e.clientX, 
    y: e.clientY,
    tolerance: 40,
    sortMinDist: true
}, '.block');

@gilmoreorless
Copy link
Owner

Hi, thanks for this PR, I like the idea. :)

A couple of things though:

  1. The same option will also be used by .furthest() and .touching() - how will this option work for those? For example, I'd say sorting by closest first doesn't make sense for .furthest(), but an opposite sort would be better.
  2. Could you add some tests as mentioned in CONTRIBUTING.md? This will make verifying the above behaviour easier.

Cheers,
Gil

@gilmoreorless
Copy link
Owner

Closing this in favour of the sort option introduced in PR #29, which is now released as version 1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants