# Thread: ranking textboxes

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

3. ## Re: ranking textboxes

Originally Posted by Shaggy Hiker
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. ## 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:
`Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Dim lb As New ListBox        Dim Ascendinglb As New List(Of Integer)        Dim ItemToAdd As Integer        For i As Integer = 1 To 5            Dim tb = Me.Controls("TextBox" & i)            If Integer.TryParse(tb.Text, ItemToAdd) Then                lb.Items.Add(ItemToAdd)                tb.Text = ""            End If        Next        If lb.Items.Count > 1 Then            Ascendinglb.Clear()            For i = 0 To lb.Items.Count - 1                Ascendinglb.Add(CInt(lb.Items(i)))            Next            Ascendinglb.Sort()            lb.Items.Clear()            For i = 0 To Ascendinglb.Count - 1                Dim tb = Me.Controls("TextBox" & i + 1)                tb.Text = Ascendinglb(i).ToString            Next        End If    End Sub`

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

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

7. ## 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:
`Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Dim lb As New ListBox        Dim Ascendinglb As New List(Of Double)        Dim ItemToAdd As Double        For i As Integer = 1 To 5            Dim tb = Me.Controls("TextBox" & i)            If Double.TryParse(tb.Text, ItemToAdd) Then                lb.Items.Add(ItemToAdd)                tb.Text = ""            End If        Next        If lb.Items.Count > 1 Then            Ascendinglb.Clear()            For i = 0 To lb.Items.Count - 1                Ascendinglb.Add(CDbl(lb.Items(i)))            Next            Ascendinglb.Sort()            lb.Items.Clear()            For i = 0 To Ascendinglb.Count - 1                Dim tb = Me.Controls("TextBox" & i + 1)                tb.Text = Ascendinglb(i).ToString                'or you could add them to a listbox sorted like this                'ListBox1.Items.Add(Ascendinglb(i))            Next        End If    End Sub`

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

#### Posting Permissions

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

Click Here to Expand Forum to Full Width