I have 7 values that I have to sort through and get the lowest value back.How can I get the lowest value back this list within c#.
How to get the Smallest value from a list of values and how to do it with c#
Collapse
X
-
Values of what? Doubles, ints, elephants?
You will want to use the Array.Sort static method.
Code:int[] iArr = { 5, 2, 3, 1, 4 }; Array.Sort(iArr);
This will work for primitives, but if you have object values, you will have to either overload their operators or sort them yourself. -
Originally posted by Sheena777I have 7 values that I have to sort through and get the lowest value back.How can I get the lowest value back this list within c#.
Set a variable equal to the value of the first element [0].
Loop through the array
if the value of this element is lower than your saved value, then the saved value becomes this value
end loopComment
-
Originally posted by insertAliasValues of what? Doubles, ints, elephants?
You will want to use the Array.Sort static method.
Code:int[] iArr = { 5, 2, 3, 1, 4 }; Array.Sort(iArr);
This will work for primitives, but if you have object values, you will have to either overload their operators or sort them yourself.
Is there any way to do this with a 2 dimensional array?Comment
-
you can sort them in order if you want using a bubble sort loop with your array
a bubble sort works kindof like this - you will need to store values in temporary containers after you compare them. if they are greater or smaller, switch them.. i hope this helps you. the end result is a sorted set of values with the one you need at either top or bottom of the arrayComment
-
i guess this is what would solve this problem.
Code://Set LowestNumber to the maximum value int LowestNumber = int.MaxValue; //Create a list you can fill with int values List<int> MyIntList = new List<int>(); //Add some values MyIntList.Add(43); MyIntList.Add(21); MyIntList.Add(6); MyIntList.Add(102); MyIntList.Add(93); MyIntList.Add(7); //Check if the value is lower then the lowest number at that moment foreach (int i in MyIntList) { if (i < LowestNumber) { LowestNumber = i; } }
Comment
Comment