|
-
Sep 3rd, 2011, 03:26 AM
#1
Thread Starter
Member
[Resolved][Help] Rank/sort a number
Hello everyone, i'm stuck with this.
i try to find fastest way, but its only fits for certain sequence of number
here is what i want:
Points Ranks
10 5
25 4
38 3
35 2
90 1
assuming that all points is on a label named lblTotPts(0 to 4)
and all Ranks is on a label named lblRank(0 to 4)
im using this code
Code:
Dim PM
For x = 1 To 5
For i = 1 To 5 - 1
If lblRank(i).Caption = "" Then
If Val(lblTotPts(i).Caption) > Val(lblTotPts(PM).Caption) Then
PM = i
End If
End If
Next
lblRank(PM).Caption = x
PM = 0
Next
it works only on some sequence of number. but its returned like this, if the first number was on rank 5.
Points Ranks
5 5
4
3
2
1
as i change the ">" sign to "<" it works for that sequence, but not for this:
Points Ranks
1 5
2
3
4
5
so, how to do that using a fast way (looping) because the actual element (lblRank and lblTotPts) on my project is not only contains 5 controls.
thanks before
Last edited by astralist; Sep 3rd, 2011 at 10:06 AM.
-
Sep 3rd, 2011, 08:20 AM
#2
Re: [Help] Rank/sort a number
May not be exactly what you are looking for but might give
you some hints. The idea below is to sort a Tag or Index Array
instead of sorting the points array. I used a bubblesort because
there are so few elements.
Code:
Option Explicit
Private Sub Form_Load()
Dim i As Long
Dim Pts(4) As Single
Dim Rnk(4) As Long
Randomize
For i = 0 To 4
Rnk(i) = i
lblRank(i).Caption = i + 1
Pts(i) = Int(Rnd * 100)
Next
BubbleSort Pts, Rnk
For i = 0 To 4
lblPoints(i).Caption = Pts(Rnk(i))
Next
End Sub
Public Sub BubbleSort(Arr As Variant, TA As Variant)
Dim i As Long
Dim j As Long
Dim Tmp As Long
For i = UBound(Arr) To LBound(Arr) Step -1
For j = LBound(Arr) + 1 To i
If Arr(TA(j - 1)) > Arr(TA(j)) Then
Tmp = TA(j - 1)
TA(j - 1) = TA(j)
TA(j) = Tmp
End If
Next
Next
End Sub
-
Sep 3rd, 2011, 09:31 AM
#3
Thread Starter
Member
Re: [Resolved][Help] Rank/sort a number
Last edited by astralist; Sep 3rd, 2011 at 10:06 AM.
-
Sep 3rd, 2011, 09:56 AM
#4
Re: [Help] Rank/sort a number
Put all the Points values in an array, then sort the array to get Rank value for each Points value. Use this array as a reference to assign Rank value to each company based on the Points value.
You can do a lot more than just that... you could sort the entire table on any of the columns. Sorting by Total Points (descending) gives you the Rank (ascending) that you desire.
-
Sep 3rd, 2011, 10:11 AM
#5
Thread Starter
Member
Re: [Resolved][Help] Rank/sort a number
well.. altough its applicable, i think its kida hard. because each text represents by one label.
except if i'm using listview, grid, or something like that, it will be easier.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|