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:
abs(diff)
, then you remove the value from the list. You repeat the previous step until you have 5 closest terms.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.