How to compare strings to get the highest number

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1194 Views - Last Post: 04 June 2019 - 12:47 AM Rate Topic: -----

#1 zecacodigo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-December 15

How to compare strings to get the highest number

Posted 15 May 2019 - 06:55 AM

Hi,

I am trying to do a simple evaluation to get the highest number of 6 strings. Imagine this:

Dim string0 = "0"
Dim string1 = "1"
Dim string2 = "2"
Dim string3 = "3"
Dim string4 = "4"
Dim string5 = "5"
Dim result = ""



As simple as that.

Now all I want to to is to compare all the strings and get the highest number on another string.

I know I could do "if string0 > string1 then..." bla bla

But I would need a lot of "if's".

There is got to be a better way.

Any ideas?

Thank you in advanced.

Is This A Good Question/Topic? 0
  • +

Replies To: How to compare strings to get the highest number

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15076
  • View blog
  • Posts: 60,205
  • Joined: 12-June 08

Re: How to compare strings to get the highest number

Posted 15 May 2019 - 06:59 AM

A function would work. A function that contains the IF and returns the highest.

Alternatively you could put them in a collection, sort, and take which ever end you deemed to be highest.
Was This Post Helpful? 1
  • +
  • -

#3 zecacodigo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-December 15

Re: How to compare strings to get the highest number

Posted 15 May 2019 - 07:22 AM

View Postmodi123_1, on 15 May 2019 - 06:59 AM, said:

A function would work. A function that contains the IF and returns the highest.

Alternatively you could put them in a collection, sort, and take which ever end you deemed to be highest.


Thanks for your reply.
I don't know how to create functions. Also, the problem of many ifs would still be present :(

The collection is not a bad idea I think. As the last number would be the highest.
But I still wonder if there is a better way...
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15076
  • View blog
  • Posts: 60,205
  • Joined: 12-June 08

Re: How to compare strings to get the highest number

Posted 15 May 2019 - 07:26 AM

No.. there would be one IF statement, in the function. That would be the most efficient.

Don't know how to make a function? Super - read up on them!
https://docs.microso...ction-statement
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is online

  • D.I.C Lover

Reputation: 615
  • View blog
  • Posts: 4,056
  • Joined: 12-January 10

Re: How to compare strings to get the highest number

Posted 15 May 2019 - 07:52 AM

you can convert the string to a number and add them to a list then use a function to test the numbers in the list to get the largest.

dim someNumber as integer = convert.toint32(yourStringGoesHere)


then add the numbers to a list

dim someList as arrayList = new arrayList

someList.add(someNumber)


then go through the list looking for the largest number

foreach n as int in someList
do something
next



that is only partial code and there are tons of ways to do it

This post has been edited by DarenR: 15 May 2019 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 124
  • View blog
  • Posts: 684
  • Joined: 01-October 08

Re: How to compare strings to get the highest number

Posted 15 May 2019 - 08:04 AM

View Postzecacodigo, on 15 May 2019 - 08:55 AM, said:

Hi,

I am trying to do a simple evaluation to get the highest number of 6 strings. Imagine this:

Dim string0 = "0"
Dim string1 = "1"
Dim string2 = "2"
Dim string3 = "3"
Dim string4 = "4"
Dim string5 = "5"
Dim result = ""



As simple as that.

Now all I want to to is to compare all the strings and get the highest number on another string.

I know I could do "if string0 > string1 then..." bla bla

But I would need a lot of "if's".

There is got to be a better way.

Any ideas?

Thank you in advanced.


If you need these strings to be compared as numbers they have to be converted to numbers. This shows why

        Dim string1 As String = "100"
        Dim string2 As String = "20"

        If string2 > string1 Then
            Stop 'this will happen if comparing strings
        End If



Strings that look like numbers are not numbers.
Was This Post Helpful? 1
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: How to compare strings to get the highest number

Posted 15 May 2019 - 09:52 AM

You should be working with integers and not strings, but since this is the way you have it, you can try this ::
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Gather your variables
        Dim myStrings() As String = {"0", "1", "10", "15", "200", "5"}

        Dim listOfStrings As New List(Of String)(myStrings)
        Dim listResult As Integer = retListHighestValue(listOfStrings, Nothing)
        Console.WriteLine("Max value for listResult is " & listResult)

    End Sub
    Public Shared Function retListHighestValue(ByVal eList As List(Of String), ByRef returnValue As Integer) As Integer
        'this #1
        For Each var As Integer In eList
            returnValue = Math.Max(returnValue, var)
        Next
        Return returnValue
    End Function

Was This Post Helpful? 2
  • +
  • -

#8 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 375
  • View blog
  • Posts: 1,603
  • Joined: 07-September 13

Re: How to compare strings to get the highest number

Posted 15 May 2019 - 10:12 AM

I am in agreement with DarenR, you should be converting your strings to a numeric type, how you do this will depend on how you want your application to act.

Daren showed you one way of converting a string to a integer, however, that way will thrown an exception if the string isn't a value that it can correctly parse for the integer. to get around this you will have to catch the exception and deal with it or precheck if you need to.

Now if you know the strings being passed to you are always going to be numbers then it doesn't overly matter if you use Integer.Parse (same thing as a Int32.Parse), if you don't and you want to check if it can be successfully parsed you can use Integer.TryParse instead, this returns a boolean to tell you if it could be successfully parsed while also doing the parse at the same time.

        Dim stringValue As String = "5"
        Dim integerValue As Integer = 0

        If Integer.TryParse(stringValue, integerValue) Then
            ' successfully parsed, the integerValue will now hold the number 5
        Else
            ' failed to parse, the integerValue will still be holding the number 0
        End If



So to take away from this you can do Integer.Parse, Integer.TryParse and you may also want to look into Try Catches to handle exceptions as they appear.

The links I provided are all in C#, however, the information is the same for both languages, you may just need to google the syntax differences for an example in VB.Net

On another note, once you have all your values converted to integers and if you store them away in a ienumerable like a List(of Integer) for example, you will be able to go listVariable.Max() to get the Max Value in the list.
Was This Post Helpful? 0
  • +
  • -

#9 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 124
  • View blog
  • Posts: 684
  • Joined: 01-October 08

Re: How to compare strings to get the highest number

Posted 16 May 2019 - 09:29 AM

Another solution that also uses integers,

    Private Function HighVal(ParamArray nums() As String) As String
        Dim rv As Integer
        rv = (From n In nums
              Let isnum = Integer.TryParse(n, Nothing)
               Where isnum
               Select Integer.Parse(n)).Max

        Return rv.ToString
    End Function


Was This Post Helpful? 0
  • +
  • -

#10 zecacodigo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-December 15

Re: How to compare strings to get the highest number

Posted 16 May 2019 - 11:03 AM

Thank you all for your comments and code examples, they were very helpful.

Also, meanwhile I found this code (from here: https://www.dotnetpe...-max-min-vbnet) :

      ' Find largest and smallest elements in array.
                        Dim vals As Integer() = {string0, string1, string2, string3, string4, string5}

                        Dim largest As Integer = Integer.MinValue
                        Dim smallest As Integer = Integer.MaxValue

                        For Each element As Integer In vals
                            largest = Math.Max(largest, element)
                            smallest = Math.Min(smallest, element)
                        Next



Which does exactly what I want.

This is solved now.

Thank you all again.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15076
  • View blog
  • Posts: 60,205
  • Joined: 12-June 08

Re: How to compare strings to get the highest number

Posted 16 May 2019 - 11:44 AM

You will not grow, or develop, as a programmer by just using someone else's code. Next time try an earnest effort into writing it yourself first.
Was This Post Helpful? 2
  • +
  • -

#12 zecacodigo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-December 15

Re: How to compare strings to get the highest number

Posted 16 May 2019 - 02:19 PM

View Postmodi123_1, on 16 May 2019 - 11:44 AM, said:

You will not grow, or develop, as a programmer by just using someone else's code. Next time try an earnest effort into writing it yourself first.


Do you feel better now?

I am not a programmer, or intend to be one, I just do this things as a hobbie.

Just because I didn't post all my approaches here, doesn't mean that I didn't try myself. I only ask anything on forums if I can't do it or if I can't find it on the Internet.

Why should I re-invent the wheel? If the code is already available online, I should use it, and not lose time trying to make it myself. This will help me understanding the code and be able to read the code itself.

I taught code myself looking for code online, copying, pasting and editing, and changed it to my needs.

For instance, once I wanted to save settings to a file, like a .txt or .ini file... I could not find an easy or simple solution online at the time. So I did it myself. Based on the experience I gained along the years from reading others peoples code. Now I can save the settings in any extension or even create my own.

What I do is probably not the best approach, or the best code, but it works. It might be a longer code, might take more or less time to execute, may consume more or less RAM... I don't know, but it works. Again, no one ever taught me to code.

I have done some programs/applications, and some are available online and people love it, but I still think I am a newbie, because I am.

If I was hoping for you (any of you) to give me the code, I would simply copy the code that some users published here and run away, not even bothering to leave a thank you.

I am not mad, I just didn't like your authority since the beginning of the post, and now again.

We all have different paths on life, remember that.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15076
  • View blog
  • Posts: 60,205
  • Joined: 12-June 08

Re: How to compare strings to get the highest number

Posted 16 May 2019 - 02:42 PM

Do you feel better now, too? ;)

My angle is this - there are basic things that help you be a better developer - be it a hobbyist or professionally. Certain building blocks that go far beyond the immediate "do X thing". In a larger sense this is what homework is for, questions at the end of chapters, and sample projects to jack around with.

Case in point - sure, the example you gave was sorting a series of strings that happened to contain numbers. Copy/pasting gets you the result, but by passes all sorts of useful nuggets. DarenR #5 post, dbasnett's #6, and sheepings' #7 all provide information on converting the strings to integers.

Having correct datatypes is a fairly crucial piece of information to have when programming. Be it inserting something into a database, dealing with user input in a format you were not expecting, to sorting, to math, ASCII backing values, and so on. Pasting in an answer doesn't provide that insight.

Further on there were some folk recommending a function to do the compare, or even a collection to sort them for you. Both options have merit, both get the job done, and both should be explored.

This is a great example for a toe-dip into the waters of functions if you haven't used them before. How one structures a function, input, output, scope, and so on. Functions are another crucial piece of software development knowledge.

Collections crop up everywhere. Sure you'll have plenty of need for one off variables, but what about a group of variables? Data from a database, JSON files, complex objects, and so on. With about fifteen minutes of reading up on them, and about another fifteen to dink around with them, you can open a new chapter of ways to pass like data, interact with like data, and so on.

Off of this is not useless trivia - it adds another tool to your toolbox of knowledge to blow past just copy/pasting what ever code you have but taking ideas, concepts, and written code to create something new. While the web is vast and wide not everything coded is out there to find. Occasionally it takes banging two different concepts together and sparking a fire. It's an issue of keeping stagnate and dependent or breaking through to critical thinking and problem solving.

I would hope you would see it from the larger picture. No one wants ot keep anyone down, but make them a better, self reliant, creator of code.

As I heard someone say, we all have different paths on life and can see it from a different point of view.

Oh, and next time please copy/paste your most recent attempts so folk know where you are at. :^:
Was This Post Helpful? 2
  • +
  • -

#14 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2853
  • View blog
  • Posts: 11,184
  • Joined: 03-December 12

Re: How to compare strings to get the highest number

Posted 17 May 2019 - 11:59 AM

:eek:
Was This Post Helpful? 0
  • +
  • -

#15 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 124
  • View blog
  • Posts: 684
  • Joined: 01-October 08

Re: How to compare strings to get the highest number

Posted 17 May 2019 - 12:59 PM

View Postzecacodigo, on 16 May 2019 - 01:03 PM, said:

Thank you all for your comments and code examples, they were very helpful.

Also, meanwhile I found this code (from here: https://www.dotnetpe...-max-min-vbnet) :

      ' Find largest and smallest elements in array.
                        Dim vals As Integer() = {string0, string1, string2, string3, string4, string5}

                        Dim largest As Integer = Integer.MinValue
                        Dim smallest As Integer = Integer.MaxValue

                        For Each element As Integer In vals
                            largest = Math.Max(largest, element)
                            smallest = Math.Min(smallest, element)
                        Next



Which does exactly what I want.

This is solved now.

Thank you all again.


If you have an array of integers, vals in the code above, then the loop is NOT needed. All you need is

        Dim largest As Integer = vals.Max
        Dim smallest As Integer = vals.Min


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2