0

i have a project in vb.net to create reports of students. my main problem is how to rank a list of students' scores and display the various students' name with their scores and position. how can i rank the students' scores and display their names,scores and position? This is what i did, which solved part of the question.

The Code is given below.

Public Class Form1
Dim numbers(4) As Integer
Dim Group1Score, Group2Score, Group3Score, Group4Score, Group5Score, Group6Score As Integer

Dim Group1Title, Group2Title, Group3Title, Group4Title, Group5Title, Group6Title As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Group1Score = 4
    Group2Score = 8
    Group3Score = 15
    Group4Score = 16
    Group5Score = 34

    numbers(0) = Group1Score
    numbers(1) = Group2Score
    numbers(2) = Group3Score
    numbers(3) = Group4Score
    numbers(4) = Group5Score

    Array.Sort(numbers)
    Array.Reverse(numbers)

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label4.Text = numbers(0)
    Label5.Text = numbers(1)
    Label6.Text = numbers(2)
    Label7.Text = numbers(3)
    Label8.Text = numbers(4)

    'Next
End Sub
End Class

any help please?

Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65

2 Answers2

1

For better representation of the scores, I have used a ListBox. Here is the code:

Public Class Form1

Dim GroupList As New List(Of Group)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    GroupList.Add(New Group("Group 1", 4))
    GroupList.Add(New Group("Group 2", 8))
    GroupList.Add(New Group("Group 3", 1))
    GroupList.Add(New Group("Group 4", 16))
    GroupList.Add(New Group("Group 5", 34))
    GroupList.Add(New Group("Group 6", 2))
    GroupList.Sort(New Comparison(Of Group)(Function(x, y) x.Score.CompareTo(y.Score)))
    GroupList.Reverse()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each item As Group In GroupList
        ListBox1.Items.Add(String.Format("{0}'s position is {1} with a score of {2}", item.Title, GroupList.IndexOf(item) + 1, item.Score))
    Next
End Sub
End Class

Public Class Group
    Public Title As String
    Public Score As Integer
    Public Sub New(_title As String, _score As Integer)
        Title = _title
        Score = _score
    End Sub
End Class

If you want to get a particular item by Name or Score, you can use the List.Find method. To find by name and display its details you would use:

 Dim group = GroupList.Find(Function(item) item.Title = "Group 2")
 Dim position = GroupList.IndexOf(group) + 1
 Label1.Text = String.Format("Name: Group 2, Score: {0}, Position:{1}", group.Score, position)
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
0

If you want to access the information by the student name you could use a dictionary (https://msdn.microsoft.com/es-es/library/xfhwa508%28v=vs.110%29.aspx) using the student name as key and an array with the rest of the information as value.