Solved

MSFlexgrid countingsort

Posted on 2012-12-28
9
374 Views
Last Modified: 2013-01-08
Dear Experts, I have a MSFlexGrid1 With 5 colums of integer data, max integer is 1 to 50.
I would like to countsort these colums, so that I end up with a list in a second MSFlexgrid2, of the counted and sorted data like the list below. In the second MSFlexgrid2 there should be only two colums as shown below. The code will need to identify the individual numbers from 1 to 50, and show how many times they are repeated in the 5 columns. I am using vb6. Your help would be appreciated, thank you.

Number    Frequency
1              80
2              65
3              45
4              90
etc           etc
50            120
0
Comment
Question by:AWaterfalls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 38729281
Try this:

Private Sub Command1_Click()

  On Error GoTo Err_Handler

  Dim c As Long
  Dim r As Long
  
  Dim nValue As Long
  Dim nCountColumns As Long
  Dim aNumers(1 To 50) As Long
  
  c = 0
  nCountColumns = 5
  
  With MSFlexGrid1
      For c = 0 To nCountColumns - 1
    
          ' Sort by Column
          .Col = c
          .ColSel = 0
          .Sort = flexSortGenericAscending
      
          nValue = 0
          
          For r = 1 To .Rows - 1
              If Len(.TextMatrix(r, .Col)) > 0 Then
                  If IsNumeric(.TextMatrix(r, .Col)) And ((.TextMatrix(r, .Col) > 0) And (.TextMatrix(r, .Col) <= 50)) Then
                      If r = 1 Then
                          nValue = Val(.TextMatrix(r, .Col))
                          
                          aNumers(nValue) = aNumers(nValue) + 1
                      Else
                          If CheckRow(r, .Col, nValue) = False Then
                              nValue = Val(.TextMatrix(r, .Col))
                          End If
                              
                          aNumers(nValue) = aNumers(nValue) + 1
                      End If
                  End If
              End If
          Next r
      Next c

  End With
  
  Call FillCountGrid(aNumers(), MSFlexGrid2)

  Exit Sub

Err_Handler:
  Debug.Print "ERROR (Command1_Click): " & CStr(Err.Number) & ", " & Err.Description

End Sub

Private Function CheckRow( _
                    ByVal nRow As Long, _
                    ByVal nCol As Long, _
                    ByVal nValue As Long _
                    ) As Boolean
  
  With MSFlexGrid1
      If nRow <= .Rows - 1 Then
          If IsNumeric(.TextMatrix(nRow, nCol)) And ((.TextMatrix(nRow, nCol) > 0) And (.TextMatrix(nRow, nCol) <= 50)) Then
              If Val(.TextMatrix(nRow, nCol) = nValue) Then
                  CheckRow = True
              End If
          End If
      End If
  End With
  
  Exit Function

End Function

Private Sub FillCountGrid( _
                ByRef aNumers() As Long, _
                ByRef oFlexGrid As MSFlexGrid _
                )
                
  On Error GoTo Err_Handler

  Dim Entry As Variant
  Dim i As Long
  Dim j As Long
                                 
  With oFlexGrid
      .Cols = 2
      .FixedRows = 1
      .Rows = .FixedRows
      
      .TextMatrix(0, 0) = "Number"
      .TextMatrix(0, 1) = "Frequency"
      
      For i = 1 To 50
          Entry = i & Chr(9) & aNumers(i)
          
          .AddItem Entry
      Next i
      
  End With

  Exit Sub
  
Err_Handler:
  Debug.Print "ERROR (FillCountGrid): " & CStr(Err.Number) & ", " & Err.Description

End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:eemit
ID: 38729340
Version 2:

Private Sub Command1_Click()

  On Error GoTo Err_Handler

  Dim c As Long
  Dim r As Long
  
  Dim nValue As Long
  Dim nCountColumns As Long
  Dim aNumers(1 To 50) As Long
  
  nCountColumns = 5
  
  With MSFlexGrid1
      nValue = 0
      
      For r = 1 To .Rows - 1
          For c = 0 To nCountColumns - 1
              If Len(.TextMatrix(r, c)) > 0 Then
                  If IsNumeric(.TextMatrix(r, c)) Then
                      nValue = Val(.TextMatrix(r, c))
                      If (nValue > 0) And (nValue <= 50) Then
                          aNumers(nValue) = aNumers(nValue) + 1
                      End If
                  End If
              End If
          Next c
      Next r
  End With
  
  Call FillCountGrid(aNumers(), MSFlexGrid2)
  
  Exit Sub

Err_Handler:
  Debug.Print "ERROR (Command1_Click): " & CStr(Err.Number) & ", " & Err.Description

End Sub

Open in new window

0
 

Author Comment

by:AWaterfalls
ID: 38731535
Version 2, Worked very well. Thank you, you get maximum points for that answer.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AWaterfalls
ID: 38731585
I've requested that this question be closed as follows:

Accepted answer: 0 points for AWaterfalls's comment #a38731535

for the following reason:

It Just Worked First Tme.
0
 
LVL 15

Expert Comment

by:eemit
ID: 38731586
Hi AWaterfalls,
Thank you,
you should accept my answer as solution.
Please read here how to close the question:
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=24
0
 
LVL 15

Expert Comment

by:eemit
ID: 38739735
Accept: eemit {http:#a38729340}

In her post {http:#a38731535}
Asker said:
"Version 2, Worked very well. Thank you, you get maximum points for that answer."

In her post {http:#a38731585}
Asker accidentally accept his own Answer:
(Version 2, Worked very well. Thank you, you get maximum points for that answer.)
Asker said as Grading Comment:
"It Just Worked First Tme."

eemit
0
 

Author Comment

by:AWaterfalls
ID: 38742527
I am sorry but I am New to all this, however the first solution gave only zeros for the frequency on the second flexgrid. While solution 2 gave the frequencies of the balls as per requested by me. Thank you for your help, and I look forward to using experts again in the future.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 74
Query group by data in SQL Server - cursor? 3 66
printf performancy 11 86
Bot application - advice 3 63
This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn different types of Android Layout and some basics of an Android App.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question