Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. Please Im trying to grade numbers to 1st. 2nd, etc. by virtue of which is greater and least.

So lets say I have an array of numbers say- 1 to 24

10
22
33
33
40
50

what I want to achieve is:

33 - Draw
50 - 1st
40 - 2nd
22 - 3rd
10 - 4th

What I have tried:

I tried something like this at first even though I know its clearly wrong. Any help will be much appreciated.

  Dim iGrades() As Integer = {"33", "50", "40", "22", "10"}
     
        For Each item In iGrades

            Try
                Dim Lst As New ListBox

                If item = item Then
                    Lst.Items.Add(item & " - Draw")
                ElseIf item > item Then
                    Lst.Items.Add(item & " - 1st")
                ElseIf item < item Then
                    Lst.Items.Add(item & " - 2nd")
                End If

            Catch ex As Exception
Msgbox(err.description)
            End Try
        Next
Posted
Updated 26-Apr-17 10:17am
v4
Comments
Maciej Los 26-Apr-17 13:50pm    
You're trying to compare strings instead of integers!
[no name] 26-Apr-17 13:55pm    
And you are comparing the strings to themselves.
And equality is == not =. = is assignment.
Michael_Davies 26-Apr-17 13:58pm    
VB = is equality
C == is equality
[no name] 26-Apr-17 14:18pm    
Ah dang! You are right. My bad for not paying attention.

First off, you aren't comparing numbers - you are comparing strings, and the sort order (which determines which string is greater than the other) is very different.
For strings, the comparison is done based on the first different character between the two strings, so you end up with this order:
1
10
11
12
...
18
19
2
20
21
...
You need to work with numbers in doer to get what you want, so your grades shouldn't be an array of strings:
Dim Grades() As Integer = {33, 50, 33, 40, 22, 10}

Then it's pretty trivial: Sort the array - that's simple Array.Sort Method (Array) (System)[^] will do it.
Then loop through the sorted array detecting the duplicates for your "Draws" - that's also simple, since after sorting they are all right next to each other! If it's not a draw, add it to a List so that you can get at the "non-draw" values next.
Then it's trivial to finish it: the values are ordered so the first is teh highest value, and so on.

Give it a try - it's not difficult!

But do yourself a favour: never swallow exceptions! A blank catch block is just asking for trouble because you have no idea that an error happened, much less what it actually was! Report errors, or log them, or don't catch them - swallowing them just makes your code harder for you to get working!
 
Share this answer
 
Seems you're very beginer...

As i mentioned in the comment to the question, there's no chance to compare strings the same way as integers. You have to use proper data type[^].

One of the way to sort array is to use Linq[^]. See:
VB.NET
Dim iGrades() As Integer = {10, 33, 40, 50, 22, 33}

Dim result = iGrades _
   .OrderByDescending(Function(x) x) _
   .GroupBy(Function(x) x) _
   .Select(Function(grp, iterator) New With _
       { _
           .Index = iterator + 1, _
           .Values = String.Join(", ", grp.Select(Function(x) x)) _
       }) _
   .ToList()

For Each g  In result
   Console.WriteLine("{0} - {1}", g.Index, g.Values)
Next


Result:
1 - 50
2 - 40
3 - 33, 33
4 - 22
5 - 10


View words about above Linq query:
.OrderByDescending() - sorts data in descending order
GroupBy() - creates groups of the same values
Select() - creates new data set, where iterator is used to get the index for each group.

Note, that when you're trying to sort strins, a result may differ. Check this:
VB.NET
Dim sGrades() As String = {"10", "33", "40", "50", "22", "33", "11", "1", "111"}

Dim result = sGrades _
   .OrderByDescending(Function(x) x) _
   .GroupBy(Function(x) x) _
   .Select(Function(grp, iterator) New With _
       { _
           .Index = iterator + 1, _
           .Values = String.Join(", ", grp.Select(Function(x) x)) _
       }) _
   .ToList()

For Each g  In result
   Console.WriteLine("{0} - {1}", g.Index, g.Values)
Next


Result:
1 - 50
2 - 40
3 - 33, 33
4 - 22
5 - 111
6 - 11
7 - 10
8 - 1


Take a look at bolded and underlined "numbers"!
But...

You have to starts with basics:
Arrays in Visual Basic[^]
How to: Sort An Array in Visual Basic[^]
Sorting Algorithms In VB[^]
 
Share this answer
 
v2
Comments
SamuelDexter 26-Apr-17 16:04pm    
First and foremost, I'm not a" very beginner" I've been coding since vb6. Here's proof. http://www.softpedia.com/publisher/SoftReckon-LLC-Samuel-Dexter-Yeboah-Gaisie-59052.html
its an oversight trust me. Im typing straight from mobile and not a copy from my main project code. I know u should have assignedal an integer to my variable since I'm working with numbers. I've even tried to edit the post but simply cannot even find the edit button.
Maciej Los 26-Apr-17 16:08pm    
Ok, i believe you.
On the right-bottom corner of your question, you'll find Improve question widget.
SamuelDexter 26-Apr-17 16:12pm    
Thanx man. Appreciated
Maciej Los 26-Apr-17 16:29pm    
You're very welcome.

Thank you for accepting my answer.
Cheers,
Maciej
Maciej Los 26-Apr-17 16:10pm    
I did it. Follow the link i provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900