c# - How to get the list index of the nearest number? -
how list index can find closest number?
list<int> list = new list<int> { 2, 5, 7, 10 }; int number = 9; int closest = list.aggregate((x,y) => math.abs(x-number) < math.abs(y-number) ? x : y);
if want index of closest number trick:
int index = list.indexof(closest);
Comments
Post a Comment