 # Thread: [RESOLVED] How to count and sort?

1. ## [RESOLVED] How to count and sort?

Dear all expert programmers,

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.  Reply With Quote

2. ## 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  Reply With Quote

3. ## 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  Reply With Quote

4. ## 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  Reply With Quote

5. ## Re: How to count and sort?

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

6. ## Re: How to count and sort?

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

7. ## 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  Reply With Quote

8. ## 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?  Reply With Quote

9. ## 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.)  Reply With Quote

10. ## 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  Reply With Quote

11. ## 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.  Reply With Quote

12. ## 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.  Reply With Quote

#### 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