Link to home
Start Free TrialLog in
Avatar of Natavia Finnie
Natavia FinnieFlag for United States of America

asked on

Is there a vb.net function that allows you to test for the lowest, intermediate and highest of 3 numbers?

I would like to test the order of 3 given numbers to make sure that they are in the order of low, intermediate and high using vb.net
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

No. There isn't such a function of of the box. You can easily write your own.

Example 1
Loop through the list, comparing each item with the next. If you lost is suppsed to be sorted in ascending order, then the next item should always be bigger than the previous item

Example 2
Create a New List(Of Long)
Add the numbers to this list
Sort this list

Loop through the list with the numbers in their original order, and compare the item at each index with the new, sorted list. If you find any mismatch, then the numbers are not sorted
You can accomplish this with LINQ fairly easily:
Module Module1
	ReadOnly values As Integer() = New Integer() {6, 9, 23, 67, 43, 64, 29, 82, 7, 99, 2}
	Sub Main()
		Dim ordered = (From i In values Order By i Select i)
		Console.WriteLine("The ordered values are: [ {0} ]", String.Join(", ", Array.ConvertAll(ordered.ToArray(), Function(x) x.ToString())))
		Dim lowest = ordered.Take(3)
		Console.WriteLine("The lowest values are: [ {0} ]", String.Join(", ", Array.ConvertAll(lowest.ToArray(), Function(x) x.ToString())))
		Dim middle = ordered.Skip(Convert.ToInt32(values.Count() \ 2) - 1).Take(3)
		Console.WriteLine("The middled values are: [ {0} ]", String.Join(", ", Array.ConvertAll(middle.ToArray(), Function(x) x.ToString())))
		Dim highest = ordered.Reverse().Take(3)
		Console.WriteLine("The highest values are: [ {0} ]", String.Join(", ", Array.ConvertAll(highest.ToArray(), Function(x) x.ToString())))

		Console.WriteLine()
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -User generated image-saige-
Ofcourse if the ordering on the last set of integers matters, then you can simply reverse it again:
Module Module1
	ReadOnly values As Integer() = New Integer() {6, 9, 23, 67, 43, 64, 29, 82, 7, 99, 2}
	Sub Main()
		Dim ordered = (From i In values Order By i Select i)
		Console.WriteLine("The ordered values are: [ {0} ]", String.Join(", ", Array.ConvertAll(ordered.ToArray(), Function(x) x.ToString())))
		Dim lowest = ordered.Take(3)
		Console.WriteLine("The lowest values are: [ {0} ]", String.Join(", ", Array.ConvertAll(lowest.ToArray(), Function(x) x.ToString())))
		Dim middle = ordered.Skip(Convert.ToInt32(values.Count() \ 2) - 1).Take(3)
		Console.WriteLine("The middled values are: [ {0} ]", String.Join(", ", Array.ConvertAll(middle.ToArray(), Function(x) x.ToString())))
		Dim highest = ordered.Reverse().Take(3).Reverse()
		Console.WriteLine("The highest values are: [ {0} ]", String.Join(", ", Array.ConvertAll(highest.ToArray(), Function(x) x.ToString())))

		Console.WriteLine()
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which now produces the following output -User generated image-saige-
Hi taviaf;

The following code snippet should do what you need.
' Test sequence, this when tested will return False 
Dim listOfNumbers As New List(Of Integer) From { 1, 100, 10 }

' Test to see if the sequence is in low to high order
Dim isSameOrder = listOfNumbers.OrderBy(Function(i) i).ToList().
                  Zip(listOfNumbers, Function(l1, l2) l1 = l2).
                  All(Function(b) b = True)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial