469,271 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How to get the Smallest value from a list of values and how to do it with c#

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#.
Oct 22 '08 #1
6 8838
Curtis Rutland
3,256 Expert 2GB
Values of what? Doubles, ints, elephants?

You will want to use the Array.Sort static method.
Expand|Select|Wrap|Line Numbers
  1. int[] iArr = { 5, 2, 3, 1, 4 };
  2. Array.Sort(iArr);
  3.  
Now iArr[0] has the smallest value.

This will work for primitives, but if you have object values, you will have to either overload their operators or sort them yourself.
Oct 22 '08 #2
tlhintoq
3,525 Expert 2GB
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#.
Plan B: If you can't alter the order of the data (ie. sort it), or don't want the overhead of sorting.

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 loop
Oct 23 '08 #3
Values of what? Doubles, ints, elephants?

You will want to use the Array.Sort static method.
Expand|Select|Wrap|Line Numbers
  1. int[] iArr = { 5, 2, 3, 1, 4 };
  2. Array.Sort(iArr);
  3.  
Now iArr[0] has the smallest value.

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?
Oct 23 '08 #4
r035198x
13,262 8TB
Is there any way to do this with a 2 dimensional array?
Give an example of what you want the sort function to do on a two dimensional array.
Oct 23 '08 #5
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 array
Oct 23 '08 #6
i guess this is what would solve this problem.

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Set LowestNumber to the maximum value
  3. int LowestNumber = int.MaxValue;
  4.  
  5. //Create a list you can fill with int values
  6. List<int> MyIntList = new List<int>();
  7.  
  8. //Add some values
  9. MyIntList.Add(43);
  10. MyIntList.Add(21);
  11. MyIntList.Add(6);
  12. MyIntList.Add(102);
  13. MyIntList.Add(93);
  14. MyIntList.Add(7);
  15.  
  16. //Check if the value is lower then the lowest number at that moment
  17. foreach (int i in MyIntList)
  18. {
  19.     if (i < LowestNumber)
  20.     {
  21.         LowestNumber = i;
  22.     }
  23. }
  24.  
Sep 18 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Rob Wire | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.