Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How can I sort an array in VB.net?

Writer Matthew Martinez

Hello guys am working on a window application in vb.net Am trying to code an array that can add only 8 best subjects grades from the 10 subjects done by a student. the ten subject grades are displayed as labels and also the TOTAL of best eight subject grades is displayed as a label. Any one to help me. am new in programming. Here is my code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' subject grades Label1.Text = 7 Label2.Text = 1 Label3.Text = 3 Label4.Text = 8 Label5.Text = 4 Label6.Text = 9 Label7.Text = 2 Label8.Text = 5 Label9.Text = 2 Label10.Text = 6 Dim gradeList As New ArrayList() gradeList.Add(Label1.Text) gradeList.Add(Label2.Text) gradeList.Add(Label3.Text) gradeList.Add(Label4.Text) gradeList.Add(Label5.Text) gradeList.Add(Label6.Text) gradeList.Add(Label7.Text) gradeList.Add(Label8.Text) gradeList.Add(Label9.Text) gradeList.Add(Label10.Text) 'sort grades in an arraylist gradeList.Sort() ' Please guys help me and modify this code to sort and SUM UP ONLY best eight done subject grades. ' I want label11.text to display the answer 'THANKS.
End Sub`
4

2 Answers

As others have suggested, use a strongly-typed collection rather than ArrayList. You probably want grades to be Integer rather than String. It probably won't matter for single-digit grades, but might sort oddly otherwise.

If you change your gradeList variable into a List(Of Integer) and turn on Option Strict, you'll notice that the label text is String. You can use CInt or Integer.Parse to convert to Integer. They aren't quite the same.

You've already sorted the list in your code, so it seems what you're really asking is "How do I sum the first 8 items in a list?"

If you think about that question, you should be able to write a For-loop that does so.

You can also do what you want to do with a one-line LINQ expression:

Dim sum As Integer = (From grade In gradeList Select grade Order By grade Descending).Take(8).Sum()
0

Sorting numbers as strings leads to problems. For example if you add somewhere 10 in your grades and sort the array list, like this:

Dim gradeList As New ArrayList()
gradeList.Add("7")
gradeList.Add("1")
gradeList.Add("3")
gradeList.Add("8")
gradeList.Add("10") '! watch out
' sort string
gradeList.Sort()
' output sorted strings
for each grade in gradeList Console.WriteLine(grade)
next grade

the output is:

1
10
3
7
8

And it is obviously wrong, because the numbers are compared as literals and everything starting with 1 is less than 3 in this case.

The problem is easily solved:

  • convert the literals to numbers (or add them directly to the list as integers)
  • put them in a list
  • sort the list (default order is ascending)
  • reverse the sorted list (make the sorting descending)
  • take the first (biggest) and the second element (second biggest) from the list and sum them

A solution using a List and LINQ could look like this:

' list of integers to be sorted
dim grades as List(of integer) = new List(of integer)
' helper variable holding the parsed grade
dim g as Integer
' convert literals if possible to integers and put them in the list
for each grade in gradeList if Integer.TryParse(grade, g) then grades.Add(g) end if
next grade
' sort the grades as numbers (default is ascending order)
grades.Sort()
' invert the order = descending order
grades.Reverse()
' take the highest two and sum them
dim sum as integer = grades(0) + grades(1)
' or using LINQ ...
'dim sum as integer = grades.Take(1).First() + grades.Skip(1).Take(1).First()
Console.WriteLine(sum)

The output is in this case:

10
8
7
3
1

and the sum is:

18
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.