Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How to sort data after using uniquevalue from IDataStatistics? The problem is I am using ArcGIS 9.3 vb6.

Edit:

Thanks kenbuja for the code. It will be useful. In details what I am trying to is this.

  1. join gdb table and feature table using IQueryDef
  2. get uniquevalue of a particular field and sort the field
  3. Export all information in excel including the uniquevaluefield and other fields also.

This can be easily done in Access through SQL/querybuilder but unfortunately I am using 9.3 vb6. Using kenbuja'r code I can go upto task 2. But I lost other field information when use IDataStatistics.

share|improve this question
1  
what do you want to do exactly? do you want to get the features in a cursor in some order of values in a field? –  Devdatta Tengshe Nov 26 '12 at 14:52

3 Answers 3

To sort data based on values in a field, ITableSort should be used:

Dim pTableSort As ITableSort
Set pTableSort = New esriGeoDatabase.TableSort
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter
pQueryFilter.WhereClause = "[STATE_NAME] like  'A%'"
With pTableSort
  .Fields = "state_name, count_name"
  .Ascending("state_name") = False
  .Ascending("count_name " ) =  True 
  .CaseSensitive("state_name") = True
  .CaseSensitive("count_name ") = True
  Set .QueryFilter = pQueryFilter
  Set .Table = pTable
End With
pTableSort.sort Nothing
Dim pCursor As ICursor
Set pCursor = pTableSort.Rows
Dim pRow As IRow
Set pRow = pCursor.NextRow
Do While Not pRow Is Nothing 
  Debug.Print pRow.Value(2) & " , " & pRow.Value(1)
  Set  pRow = pCursor.NextRow

Loop 
share|improve this answer

Here's some code I came up with to sort the unique values of an IDataStatistics, written in VBA

Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pFeatureLayer As IFeatureLayer
Dim pCursor As ICursor
Dim pData As IDataStatistics
Dim penumLayer As IEnumLayer
Dim pEnumVar As IEnumVariantSimple, value As Variant
Dim pCollection As New Collection
Dim pSortedCollection As Collection
Dim i As Integer

Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
Set pFeatureLayer = pMap.Layer(0)

Set pCursor = pFeatureLayer.Search(Nothing, False)
Set pData = New esriGeoDatabase.DataStatistics
pData.Field = "Name"
Set pData.Cursor = pCursor
Set pEnumVar = pData.UniqueValues
value = pEnumVar.Next
Do Until IsEmpty(value)
  pCollection.Add (value)
  value = pEnumVar.Next
Loop

Set pSortedCollection = SortCollection(pCollection)
For i = 1 To pSortedCollection.Count
  cbotehsil.AddItem pSortedCollection.Item(i)
Next i

Public Function SortCollection(ByVal c As Collection) As Collection
   Dim n As Long: n = c.Count
   If n = 0 Then Set SortCollection = New Collection: Exit Function
   ReDim Index(0 To n - 1) As Long                    ' allocate index array
   Dim i As Long, m As Long
   For i = 0 To n - 1: Index(i) = i + 1: Next         ' fill index array
   For i = n \ 2 - 1 To 0 Step -1                     ' generate ordered heap
      Heapify c, Index, i, n
   Next
   For m = n To 2 Step -1                             ' sort the index array
      Exchange Index, 0, m - 1                        ' move highest element to top
      Heapify c, Index, 0, m - 1
   Next
   Dim c2 As New Collection
   For i = 0 To n - 1: c2.Add c.Item(Index(i)): Next  ' fill output collection
   Set SortCollection = c2
End Function

Private Sub Heapify(ByVal c As Collection, Index() As Long, ByVal i1 As Long, ByVal n As Long)
   ' Heap order rule: a[i] >= a[2*i+1] and a[i] >= a[2*i+2]
   Dim nDiv2 As Long: nDiv2 = n \ 2
   Dim i As Long: i = i1
   Do While i < nDiv2
      Dim k As Long: k = 2 * i + 1
      If k + 1 < n Then
         If c.Item(Index(k)) < c.Item(Index(k + 1)) Then k = k + 1
      End If
      If c.Item(Index(i)) >= c.Item(Index(k)) Then Exit Do
      Exchange Index, i, k
      i = k
  Loop
End Sub

Private Sub Exchange(Index() As Long, ByVal i As Long, ByVal j As Long)
   Dim Temp As Long: Temp = Index(i)
   Index(i) = Index(j)
   Index(j) = Temp
End Sub
share|improve this answer
    
Thanks kenbuja for the code. It will be useful. In details what I am trying to is this. 1. join gdb table and feature table using IQueryDef; 2. get uniquevalue of a particular field and sort the field; 3. Export all information in excel including the uniquevaluefield and other fields also. This can be easily done in Access through SQL/querybuilder but unfortunately I am using 9.3 vb6. Using kenbuja'r code I can go upto task 2. But I lost other field information when use IDataStatistics. –  Khaled Nov 27 '12 at 10:32

Oh. things are sorted. I did not know I could use Distinct command with QueryDef.

    Dim pQueryDef As IQueryDef
    Set pQueryDef = pFeatureWorkspace.CreateQueryDef

    pQueryDef.Tables = "FeatureTableName"
    pQueryDef.SubFields = "Distinct(Field1), Field2, Field3"
    .....
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.