PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
ranking textboxes-VBForums
Results 1 to 8 of 8

Thread: ranking textboxes

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    63

    ranking textboxes

    I have five textboxes that contain tendered prices. tbAAH, tbCol, tbDE, tbLex, tbUni.

    I need to rank these from lowest to highest, allowing for the fact that
    a) some may be empty
    b) some may contain the same prices

    in other words:
    tbAAH.text contains 14.67 this would be number 5
    tbCol contains 10.95 therefore this would be number 2
    tb DE contains 10.95 but this would also be number 2
    tbLex contains 9.85 therefore this would be number 1
    tbUni contains 13.17 this would be number 4

    The above assumes that each tb is populated, but it could equally be that two of the boxes are empty.

    I've tried putting them into an array and using Array.sort and it kinda works; but doesn't allow for the situations mentioned above.

    Can anyone help?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,317

    Re: ranking textboxes

    Well, Sort does seem like a good approach, but it doesn't deal with matches the way you show you want to. What you might do is to start out with the Array.Sort method to get them into an order from highest to lowest, then take a second pass through the array giving them rank ordinals. The bigger question is where to put those.

    What comes to mind at first would be to create a Dictionary(of X, Integer) where the Integer is the ordinal, but what should the X be? One option would be to make it Double, and have a value in there, except that won't work because you can't have duplicate keys. Another option would be to have a List(of List(of Double)) so that item 0 in the outer list is the rank, while the inner list held all the values. That's pretty silly, though, because there isn't much point in having a list holding 10.95, 10.95.

    Yet another option would be a Dictionary(of Integer,Decimal) where the keys would be the ranks 5, 4, 3, 2, 1, and the values would be the numbers. You might not have values for all ranks, just as you don't have a 3 in your example, but that wouldn't be too bad. It's also a bit silly, though, unless all you really want is the number.

    What might be a better option is to put the rank number into the .Tag of the Textbox. For this, you'd put the values in an array, sort them, then iterate through the array. The highest value gets a 5, if the next value is the same as the first, then it also gets a 5, otherwise the rank gets decremented and the next value gets that new rank. Putting those back into the .Tag property of the textboxes wouldn't be difficult, though it all depends on whether you need to retain which textbox had the rank, or if you just needed to know which value had the rank.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    63

    Re: ranking textboxes

    Quote Originally Posted by Shaggy Hiker View Post
    Well, Sort does seem like a good approach, but it doesn't deal with matches the way you show you want to. What you might do is to start out with the Array.Sort method to get them into an order from highest to lowest, then take a second pass through the array giving them rank ordinals. The bigger question is where to put those.

    What comes to mind at first would be to create a Dictionary(of X, Integer) where the Integer is the ordinal, but what should the X be? One option would be to make it Double, and have a value in there, except that won't work because you can't have duplicate keys. Another option would be to have a List(of List(of Double)) so that item 0 in the outer list is the rank, while the inner list held all the values. That's pretty silly, though, because there isn't much point in having a list holding 10.95, 10.95.

    Yet another option would be a Dictionary(of Integer,Decimal) where the keys would be the ranks 5, 4, 3, 2, 1, and the values would be the numbers. You might not have values for all ranks, just as you don't have a 3 in your example, but that wouldn't be too bad. It's also a bit silly, though, unless all you really want is the number.

    What might be a better option is to put the rank number into the .Tag of the Textbox. For this, you'd put the values in an array, sort them, then iterate through the array. The highest value gets a 5, if the next value is the same as the first, then it also gets a 5, otherwise the rank gets decremented and the next value gets that new rank. Putting those back into the .Tag property of the textboxes wouldn't be difficult, though it all depends on whether you need to retain which textbox had the rank, or if you just needed to know which value had the rank.
    Wow. I knew it would be complicated!!

    The plan is to have a textbox alongside the original one and for this to contain the ranked number. I'll have a go at your .tag suggestion. Not sure how to code it but thanks for the help.

  4. #4
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: ranking textboxes

    This should do the trick you will have to modify it slightly to work with the names you have set your textboxes to though, I just did it this way to save me some time so I wouldn't have to repeat the same code a few times for those 5 textboxes.

    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.         Dim lb As New ListBox
    3.         Dim Ascendinglb As New List(Of Integer)
    4.         Dim ItemToAdd As Integer
    5.         For i As Integer = 1 To 5
    6.             Dim tb = Me.Controls("TextBox" & i)
    7.             If Integer.TryParse(tb.Text, ItemToAdd) Then
    8.                 lb.Items.Add(ItemToAdd)
    9.                 tb.Text = ""
    10.             End If
    11.         Next
    12.         If lb.Items.Count > 1 Then
    13.             Ascendinglb.Clear()
    14.             For i = 0 To lb.Items.Count - 1
    15.                 Ascendinglb.Add(CInt(lb.Items(i)))
    16.             Next
    17.             Ascendinglb.Sort()
    18.             lb.Items.Clear()
    19.             For i = 0 To Ascendinglb.Count - 1
    20.                 Dim tb = Me.Controls("TextBox" & i + 1)
    21.                 tb.Text = Ascendinglb(i).ToString
    22.             Next
    23.         End If
    24.     End Sub
    Last edited by Vexslasher; Nov 28th, 2016 at 07:13 PM.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,317

    Re: ranking textboxes

    The .Tag property is a useful little item that all controls have. It holds type Object, so it can hold anything you want it to hold. However, if the plan is to have a second textbox (a label would be better, since the user can't change the information, supposedly) beside the first, then the .Tag isn't needed. You could go into interesting contortions on this, but with only five items, it barely matters.

    Vexslasher has one example, except that you need to be using Decimal rather than Integer. Double would also work, except that you sound like you are dealing with currency, so Decimal is more appropriate.
    My usual boring signature: Nothing

  6. #6
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,664

    Re: ranking textboxes

    Hi Pianoman,

    This all looks very complicated. Me, I'm all for doing things as simply as possible.

    Have you considered leaving the TextBoxes where they are and just putting their text in ranking order ?


    Poppa.

    Along with the sunshine there has to be a little rain sometime.

  7. #7
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: ranking textboxes

    Oh I'm sorry I did integers instead of doubles here I changed them for you it should work with those types of numbers like you provided now. I'm sorry I didn't notice my error sooner.
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.         Dim lb As New ListBox
    3.         Dim Ascendinglb As New List(Of Double)
    4.         Dim ItemToAdd As Double
    5.         For i As Integer = 1 To 5
    6.             Dim tb = Me.Controls("TextBox" & i)
    7.             If Double.TryParse(tb.Text, ItemToAdd) Then
    8.                 lb.Items.Add(ItemToAdd)
    9.                 tb.Text = ""
    10.             End If
    11.         Next
    12.         If lb.Items.Count > 1 Then
    13.             Ascendinglb.Clear()
    14.             For i = 0 To lb.Items.Count - 1
    15.                 Ascendinglb.Add(CDbl(lb.Items(i)))
    16.             Next
    17.             Ascendinglb.Sort()
    18.             lb.Items.Clear()
    19.             For i = 0 To Ascendinglb.Count - 1
    20.                 Dim tb = Me.Controls("TextBox" & i + 1)
    21.                 tb.Text = Ascendinglb(i).ToString
    22.                 'or you could add them to a listbox sorted like this
    23.                 'ListBox1.Items.Add(Ascendinglb(i))
    24.             Next
    25.         End If
    26.     End Sub
    Last edited by Vexslasher; Nov 30th, 2016 at 04:14 PM.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    63

    Re: ranking textboxes

    Thanks for all the help. Finally got it sorted.

    Now have to figure out how to deal with the sort when two or more boxes hold the same values.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width