Results 1 to 5 of 5

Thread: [Resolved][Help] Rank/sort a number

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    33

    Resolved [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.

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    33

    Resolved Re: [Resolved][Help] Rank/sort a number

    thanks to your help! but your code was for sorting elements that causing the place to have changed. and what i need isn't like that.
    its actually like this, to know the ranks for company, based on its total points


    i've never using any of sorting algorithm before, and after searching this forums, i found that using sorting algorithm will make the job more difficult.

    after thinking, then i have resolved it using ridiculous algorithm and loop only! look at this

    Code:
        Dim FIN, TC
        TC = 4 'Total Company
        For x = 0 To TC - 1
            lblTotPts(x).Tag = lblTotPts(x).Caption
            lblRank(x).FontBold = False
            lblRank(x).ForeColor = &H80000012
        Next
        FIN = 0
        Do
            For x = 0 To TC - 1
                lblTotPts(x).Tag = Val(lblTotPts(x).Tag) - 1
                If lblTotPts(x).Tag = "0" Then
                    FIN = FIN + 1
                    lblRank(x).Caption = TC - FIN + 1
                    If lblRank(x).Caption = "1" Then
                        lblRank(x).FontBold = True
                        lblRank(x).FontUnderline = True
                        lblRank(x).ForeColor = &HFF0000
                    End If
                End If
            Next
        Loop Until FIN = TC
    The idea is to copy all of caption to their tag. then the tag is substracted by 1 trough looping. so who's first reach zero, that's rank 1, second is rank 2, and so on
    Last edited by astralist; Sep 3rd, 2011 at 10:06 AM.

  4. #4
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    33

    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
  •  



Click Here to Expand Forum to Full Width