c# - List <T> Add Value Only If It Is Minimum Of All Elements -
i have 2 statements follows plot graph.
public static list<keyvaluepair<int, double>> entries = new list<keyvaluepair<int, double>>(); entries.add(new keyvaluepair<int, double>(j, min));
what want min
value gets added list if less or equal every other min
values in list. how can achieved? thanks.
just calculate min value of entries , check if it's bigger min
value:
if (!entries.any() || min <= entries.min(e => e.value)) entries.add(new keyvaluepair<int, double>(j, min));
update: need check if there elements in entries list, before trying find minimal value.
hint: if use rule adding items , don't modify collection in other way (i.e. remove items or insert @ index) can check last item's value - less or equal other values.
update: instead of usage keyvaluepair data, can create custom class descriptive names properties , class (you can consider better names):
public class point { public point(int iteration, double value) { iteration = iteration; value = value; } public int iteration { get; private set; } public double value { get; private set; } }
and create class encapsulates points , behavior related these points. description like:
public class decresinggraph { private list<point> points = new list<point>(); public void add(point point) { if (!points.any()) { points.add(point); return; } if (point.value <= points.last().value) points.add(point); } public ienumerable<point> points { { return points; } } }
Comments
Post a Comment