any function to find maximum number?

izza_azhar

Member
Joined
Mar 1, 2006
Messages
17
Programming Experience
Beginner
hye all,
i have several different value. is there any function to get the maximum value instead of using if.. else?
example: input = 10, 20, 56, 87, 54 1, 2, 14, 22, ..
max value = 87 rite? --> so, how to get this value?
 
Not sure if you want to use a loop, but this is how Id do it

VB.NET:
        Dim bStr As New Specialized.StringCollection
        Dim MaxValue As Int32
        bStr.AddRange("10, 20, 56, 87, 54, 1, 2, 14, 22".Split(","c))
        For Each t As Int32 In bStr
            If t > MaxValue Then MaxValue = t
        Next
        Debug.Print(MaxValue.ToString)
 
To avoid using a loop

You can also just call the System.Array.Sort command and it would go like this:

'simply call the method like this on an array of integers (ints())

System.Array.Sort(ints)

I am assuming this sorts the array least to greatest but you should double check. This also works for datetime objects and may even sort strings alphanumerically.
 
As ss7thirty said you should use the system.Array.Sort, then use the system.array.reverse to put filp the sort and make your largest number the (0) index
As here

VB.NET:
[SIZE=2][COLOR=#008000]'If your starting list is known as numbers
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] maxvalue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ary [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]() = {10, 20, 56, 87, 54, 1, 2, 14, 22}
ary.Sort(ary)
ary.Reverse(ary)
maxvalue = ary(0)
 
[/SIZE][SIZE=2][COLOR=#008000]'what you have to do if your starting list comes in as a string
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] str [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "10, 20, 56, 87, 54, 1, 2, 14, 22"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] str2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] c [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] maxvalue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ary [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]() = {0}
str2 = Split(str, ",")
[/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] c < str2.Length
ary(c) = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](str2(c), [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
c += 1
[/SIZE][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Preserve[/COLOR][/SIZE][SIZE=2] ary(c)
[/SIZE][SIZE=2][COLOR=#0000ff]Loop
[/COLOR][/SIZE][SIZE=2]ary.Sort(ary)
ary.Reverse(ary)
maxvalue = ary(0)
[/SIZE]

This of course assumes your string wil ONLY have integers in it after the split. You WILL get errors if str = "3a, 6a, 56a, 45a" or something similar. Now if you create a string array instead and then do the sort then the "3a, 6a, ecetera will only somewhat work..

In any sort of alpha numeric values a list like so (36, 3a, 78, 7, 8, 300, 101, 11a) will be sorted as follows (101, 11a, 36, 300, 3a, 7, 78, 8) everyone can prety much agree that 300 should be the largest of the numbers. However no built in sort of the array object will ever return that. Your ONLY option then is to build a custom sorter that will sort based on your business's rules.

Hope this helps!
 
Back
Top