Results 1 to 7 of 7

Thread: [RESOLVED] sort array by repeat value

  1. #1

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Resolved [RESOLVED] sort array by repeat value

    hi experts,
    i need to sort an array by repeated values, any idea or example?

    say for example
    if array value is
    A,B,C,B,A,B,C

    result shuld be
    A,A,B,B,B,C,C

    thanks
    with regards
    seenu...
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  2. #2

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: sort array by repeat value

    Seenu

    Your request is a little ambiguous ...
    1. Do you just want to sort "alphabetically"?
    2. Do you want to sort based on the "seed" -- eg:
      • A,B,C,B,A,B,C becomes A,A,B,B,B,C,C
      • C,B,A,B,C,B,A becomes C,C,B.B.B,A,A
    3. Will there always be only 1 letter?

    EDIT:
    BTW, I second RhinoBull's suggestion

    Spoo
    Last edited by Spoo; Jun 18th, 2011 at 08:50 AM.

  4. #4

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: sort array by repeat value

    thanks for the reply,
    spoo,
    not alphabetically, i just shown for example, i need to sort by repeat values of any string
    just like
    seenu
    spoo
    rhino
    spoo
    rhino
    seenu

    result wil be
    seenu
    seenu
    spoo
    spoo
    rhino
    rhino
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  5. #5

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: sort array by repeat value

    bubble sort works fine with alphabetically too, thanks rhino and spoo
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: sort array by repeat value

    Seenu

    OK, then that (your post #4) is what I would refer to as -- based on a "seed"

    That is, based on the chronological order each unique value is encountered,
    and then "grouped".

    Here is something that springs to mind -- concept:
    1. Go through the list, store unique values in chronological order
      as they are encountered in a 2-D array
    2. Also keep track of how many times repeat values are encountered
    3. Then dump the contents into a 1-D array, grouped chronologically


    Code:
    Dim aaSrc(6)
    Dim aaUni(3, 2)
    Dim aaDump(6)
    ' 1. populate aaSrc
    aaSrc(1) = "seenu"
    aaSrc(2) = "spoo"
    aaSrc(3) = "rhino"
    aaSrc(4) = "spoo"
    aaSrc(5) = "rhino"
    aaSrc(6) = "seenu"
    ' 2. populate unique
    uni = 0                                       ' default unique counter
    For ii = 1 to 6
        hv = 0                                    ' "have a match" flag
        ' 2.1. have a match
        For jj = 1 to 3
            If aaUni(jj, 1) = aaSrc(ii) Then
                hv = 1     ' set flag
                aaUni(jj, 2) = aaUni(jj, 2) + 1   ' increment count of this name
            End If
        Next jj
        ' 2.2. new name encountered
        If hv = 0 Then
            uni = uni + 1
            aaUni(uni, 1) = aaSrc(ii)       ' 1 name
            aaUni(uni, 2) = 1               ' 2 count
        End If
    Next ii
    ' 3. populate dump
    nn = 0
    For ii = 1 to 3
        ct = aaUni(ii, 2)
        For jj = 1 to ct
            nn = nn + 1
            aaDump(nn) = aaUni(ii, 1)
        Next jj
    Next ii
    For simplicity, I have hardwired array dimensions to match your
    example. Natch, you'll need to make your algo a little more flexible

    Hope that gives you some ideas

    Spoo
    Last edited by Spoo; Jun 18th, 2011 at 09:40 AM. Reason: to clarify what "that" is, seeing as you snuck in another post

  7. #7

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: sort array by repeat value

    spoo, thanks for the effort
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


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