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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sheena777
    New Member
    • Dec 2007
    • 73

    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#.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    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);
    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.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      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

      Comment

      • Sheena777
        New Member
        • Dec 2007
        • 73

        #4
        Originally posted by insertAlias
        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);
        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?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          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.

          Comment

          • econVictim
            New Member
            • Oct 2008
            • 13

            #6
            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

            Comment

            • silvertech
              New Member
              • Sep 2010
              • 1

              #7
              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

              Working...