dcsimg
Results 1 to 12 of 12

Thread: [RESOLVED] How to count and sort?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    222

    Resolved [RESOLVED] How to count and sort?

    Dear all expert programmers,

    Please help me for create code.

    Code:
    Private sub CountandSort(byval strText as string)
        ....
    End sub
    Example
    Call CountandSort("12 13 14 15 16 17 17 21")

    Output
    12 = 2
    17 = 2
    13 = 1
    14 = 1
    15 = 1
    16 = 1

    Thank you for all posts.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,514

    Re: How to count and sort?

    Nnnnno.... that's not how it works here... this feels like a homework assignment. That means we're not going to write code for you... you need to put forth some effort and we'll help with the bits that aren't working or that you can't quite get. but we're not a development shop here. We're not going to do your work for you.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,192

    Re: How to count and sort?

    First, do an instr() function to find each occurrence of your 2-character 'number'--look up that function in MSDN-easy to use.
    Record the number of times you find that 'number'.
    You could put both the 'number, (which is really a string) and the number of times you find it into a variant array, and then sort the array. (or you could simply append the string of that number to the two-character 'number' like this:

    MyString = str(numOccurances) & mySearchString. E.e.: "2" & "12", or IOWs 212
    Do this for each 'number' you find, add them into a list boxes with a sorted=true property, and you'll have it.

    There are SO many ways to do this (arrays probably the best---but I'm guessing because you didn't really post an attempt of your code, you may not be that familiar with arrays.

    We can do this FOR you, but it would be a better learning experience if you'd attempt, and then post your errors/problems

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,973

    Re: How to count and sort?

    Actually I would suggest that you use Split() to split the numbers into an array then sort the array

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,192

    Re: How to count and sort?

    Yup -split would be better. Hopefully standardusr takes the advice above to work on it himself

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    222

    Re: How to count and sort?

    Oh, I forgot to use split function. Thank you for all suggestion.

  7. #7
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: How to count and sort?

    Surely these solutions are far too complicated.

    Use the REPLACE string method.

    strText = Replace(strText,"12","2") 'replace 12 by 2

    strText = Replace(strText,"13","1") 'replace 13 by 1

    etc etc.

    This will change strText to the answer you need : 2 1 ..... etc.

    Note that you have not defined what 21 should become in your question.

    Now for you to write the homework code.

    camoore

    Wales, UK
    Last edited by camoore; Oct 8th, 2012 at 07:09 PM. Reason: typo

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,192

    Re: How to count and sort?

    Camoore...what he gave was an example.....you don't know how many '12's there will...gotta count them first.....
    But, you are right in one aspect.....all these are complicated for those who simply ask for a solution and don't bother to read books, attend classes, or practice, practice, practice...don't you agree?

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,514

    Re: How to count and sort?

    Where the heck did REPLACE come from?

    here's the logic:
    4) create a collection
    5) split the numbers
    6) loop through each number in the results
    7) see if it's in the collection
    7a) If not, add it using the number as the key and 1 as the value
    7b) if it does, then add 1 to the current vlue
    8) loop until you're out of numbers
    9) display numbers
    -tg

    (yes I know the list starts at 4... that's because 1-3 involves taking over the world.)
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: How to count and sort?

    I misunderstood the question. This was because the last number given in the sequence was 21 whereas I now think it should have been 12. Apologies for my confusion.

    The problem can still be solved by String manipulation using the Instr(N,strText,NUM) function. The op does not say in what range the numbers, of which he gives examples, will fall. Suppose it is a range of 10 to 20.

    a. Set an integer NUM to 10. Define another integer N. Define a string variable called "ANSWER"

    b. Set another Integer C to 0.

    c. Set N initially to 1

    d. If instr(N,strText,NUM) = 0 then goto step i. 'no (further) instance of NUM in strText

    e. C = C+1 'increase count C by 1

    f. Set N = Instr(N,strText,NUM) 'N becomes the character no in string of last found NUM

    g. Set N=N+2 'will be character no of the character immediately following last NUM found

    h. Loop back to step d.

    i. C is now the total number of instances found of NUM in strText

    j. ANSWER = NUM & " = " & C & ", " 'write result for NUM eg. 12 = 2, into ANSWER

    k. Increase NUM by 1

    l. If NUM = 21 then go to step n.

    m. Loop back to step b. and count the number of instances of next NUM in strText

    n. Finish routine. String ANSWER will now be, eg., 10 = 2, 11 = 1, 12 = 2, 13 = 0,......20 = 1

    That could be the logic of a solution. I have specifically not wtitten the full code because I think that the op must make that effort. If he has a problem after writing the code, he can always post it here and seek further help.

    ca moore

    Wales, UK
    Last edited by camoore; Oct 9th, 2012 at 04:28 AM.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,973

    Re: [RESOLVED] How to count and sort?

    I would not do any string functions. You could do it that way but a simple split() will break out all the numbers into an array, a sort will put them in order then it is just a matter of stepping through the array and checking the values.

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] How to count and sort?

    As OP has marked this as resolved, i'd point out that the Sort is not trivial. Looking at the example given the results are sorted in descending order of frequency and ascending order of value.

    Also, it may have been worth exploring finding the max and min of the values and using 'Dim intValues(Min To Max)' and use that array to store the frequencies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width