Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Member
 
Join Date: Dec 2007
Posts: 73
#1: Oct 22 '08
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#.

insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: Oct 22 '08

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


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.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#3: Oct 23 '08

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


Quote:

Originally Posted by Sheena777

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
Member
 
Join Date: Dec 2007
Posts: 73
#4: Oct 23 '08

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


Quote:

Originally Posted by insertAlias

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?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Oct 23 '08

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


Quote:

Originally Posted by Sheena777

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.
econVictim's Avatar
Newbie
 
Join Date: Oct 2008
Location: Chicago
Posts: 13
#6: Oct 23 '08

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


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
Reply