如何在将来和指定期限内获得最接近的X项

How to get 5 closest terms to my specified term? There is list of dates and my specified term. Ex.

My specified term: 10.7.
List:
15.6.
17.6.
24.6.
8.7.
15.7.
22.7.
5.8.
6.8.

Expected result:

17.6. (23. days back), 
24.6. (16. days back), 
8.7. (2 days back),
15.7. (5 days after my specified term), 
22.7. (12 days after my specified term)

There are two approaches to solve this problem:

  1. You get the closest term by abs(diff), then you remove the value from the list. You repeat the previous step until you have 5 closest terms.
  2. You create a list which contain the index of the value in list and the abs(diff). You sort the list and you can get 5 closest terms by picking the first 5 items from the list.

Hope I helped.