How to join all objects that are close to each other?

I have over 500 objects and I want to join all objects that are close to each other to one object.
For example I have several hair strangs and I want to join all strangs to individual hairstyles without clicking each hair strang.

If they all have the same material you can select them by material. There are some other select options in the select menu as well.