Hi.
I'm relatively new to VB and I have a question that needs an immediate response.Can anybody help me with this....

Suppose a sequential file contains the information shown below. I need help to write a program to use the file and produce a new list where the teams are in descending order by the percentage of games won,and also displaying the winning percentage

Team Won Lost
Baltimore 69 93
Boston 96 66
new York 94 68
Tampa Bay 66 96
Toronto 83 79

If the file is exactly as you posted and each line does not have any extra spaces added to the end of the line, the project should respond as asked.

Copy and Paste the entire source code in a new Project.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// customize Form.
        Me.Size = New Size(415, 300) : Me.Icon = SystemIcons.Information : Me.MaximizeBox = False
        '// create and customize a ListView.
        Dim myListView As New ListView With {.Location = New Point(0, 0), .Dock = DockStyle.Fill, .View = View.Details, _
                                                             .FullRowSelect = True, .Font = New Font("Verdana", 10)}
        With myListView.Columns '// (column name, column width, column alignment)
            .Add("Rank", 45, HorizontalAlignment.Center) : .Add("Team", 150) : .Add("Won", 45, HorizontalAlignment.Center)
            .Add("Lost", 45, HorizontalAlignment.Center) : .Add("Percentage", 90, HorizontalAlignment.Center)
        End With
        '// add ListView to Form.
        Me.Controls.Add(myListView)

        Dim sortArray As New ArrayList '// array list to store modified strings (file lines).
        Dim tempString As String = String.Empty  '// string for modifying array strings.
        Dim myCoolFile As String = "C:\test.txt" '// your file.

        If IO.File.Exists(myCoolFile) Then '// check if file exists.
            Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// load your file lines as string arrays.
            For i As Integer = 0 To myCoolFileLines.Count - 1 '// for each array.
                Dim myArray() As String = myCoolFileLines(i).Split(" ") '// separate each line into arrays by space.
                If myArray.Length = 4 Then '// if array contains a City with 2 words.
                    tempString = myArray(2) & "#" '// add games won.
                    tempString &= myArray(3) & "#" '// add games lost.
                    tempString &= myArray(0) & " " & myArray(1) '// add City.
                ElseIf myArray.Length = 3 Then '// if single word City.
                    tempString = myArray(1) & "#" '// add games won.
                    tempString &= myArray(2) & "#" '// add games lost.
                    tempString &= myArray(0) '// add City.
                End If
                '// add 0's as needed for the ArrayList to sort properly.  example: 2 = 002, 25 = 025
                Dim tempArray() As String = tempString.Split("#") '// separate games won from string.
                If tempArray(0).Length = 1 Then '// if games won are less than 10.
                    tempArray(0) = "00" & tempArray(0) '// add 2 0's to games won.
                ElseIf tempArray(0).Length = 2 Then '// if games won are less than 100 but greater than 10.
                    tempArray(0) = "0" & tempArray(0) '// add a 0 to games won.
                End If
                '// set string with new values.
                Dim finalString As String = tempArray(0) & "#" & tempArray(1) & "#" & tempArray(2)
                sortArray.Add(finalString) '// add to array list in format of: games won#games lost#City
            Next

            sortArray.Sort() '// sort the list by games won.
            Dim rank As Integer = 0
            For i As Integer = sortArray.Count - 1 To 0 Step -1 '// go thru array list from last item to first.
                '// remove the 0's from the start of a number if needed.  example: 002 = 2, 025 = 25
                Dim tempArray() As String = sortArray.Item(i).Split("#") '// separate games won.
                If tempArray(0).Substring(0, 2) = "00" Then '// check if first 2 numbers are 0's.
                    tempArray(0) = tempArray(0).Substring(2, 1) '// only get the 3rd number.
                ElseIf tempArray(0).Substring(0, 1) = "0" Then '// check if first number is 0.
                    tempArray(0) = tempArray(0).Substring(1, 2) '// get the last 2 numbers.
                End If
                '// add + 1 to rank number.
                rank += 1
                '// set equal rank numbers for teams with tied games won.
                For Each itm As ListViewItem In myListView.Items '// loop thru ListView.
                    If itm.SubItems(2).Text = tempArray(0) Then '// if ListView already contains an item that equals in games won.
                        rank -= 1 '// subtract from rank to display same rank as the previous team.
                    End If
                Next
                Dim newItem As New ListViewItem(rank) '// add rank.
                newItem.SubItems.Add(tempArray(2)) '// add city.
                newItem.SubItems.Add(tempArray(0)) '// add games won.
                newItem.SubItems.Add(tempArray(1)) '// add games lost.
                tempString = tempArray(0) / tempArray(1) * 100 '// get percentage.
                Dim percentage() As String = tempString.Split(".") '// get whole percentage from percentage.
                newItem.SubItems.Add(percentage(0) & "%") '// add percentage.
                myListView.Items.Add(newItem) '// add item to ListView.
            Next
        End If
        myListView.Select()
    End Sub

End Class

The sample project also contains code to Sort ArrayList numbers properly, which I was having trouble to locate online while piecing this project together.

Let me know if this helps. :)

I have just retested the sample project and it looks like It causes a slight error when targeting a .Net Framework less than 3.5.
This line of code,

For i As Integer = 0 To myCoolFileLines.Count - 1 '// for each array.

should actually be.

For i As Integer = 0 To myCoolFileLines.Length - 1 '// for each array.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.