Sorting in memory layer by descending order

884
3
11-02-2012 08:33 AM
DavidClarke1
New Contributor III
I am creating an in memory layer that I wish to sort.  I've tried to search the forums and have had no luck.  The documentation says there are some limitations with in memory layers but it does not list this specifically.  I followed the code sample given by the ESRI documentation but it does not seem to work.  The first record the cursor returns in the first record in the table; based on the sort I am attempting, it should be the last record returned.  The values are date/time stored in a field defined as (string).  Here are some examples of the values:
20121101143359
20121101143400

I saw a few samples where people set the "Table" property before or after setting the "Fields" and "Ascending" properties.  Notice I am trying to sort by descending order.  I get no errors.

        Dim perimeterClass As ESRI.ArcGIS.Geodatabase.IFeatureClass
        Dim geoFeatLayer As ESRI.ArcGIS.Carto.IGeoFeatureLayer
        Dim featureCursor As ESRI.ArcGIS.Geodatabase.ICursor
        Dim feature As ESRI.ArcGIS.Geodatabase.IFeature
        Dim currentIndex As Int32 = 0
        Dim tableSort As ESRI.ArcGIS.Geodatabase.ITableSort

        perimeterClass = _perimeterLayer.FeatureClass
        geoFeatLayer = _perimeterLayer

        If geoFeatLayer Is Nothing OrElse geoFeatLayer.FeatureClass.FeatureCount(Nothing) < MaxCalls Then
            Return
        End If

        tableSort = New ESRI.ArcGIS.Geodatabase.TableSortClass()

        tableSort.Fields = _keyFieldName
        tableSort.Ascending(_keyFieldName) = False
        tableSort.Table = CType(perimeterClass, ESRI.ArcGIS.Geodatabase.ITable)
        tableSort.Sort(Nothing)

        featureCursor = tableSort.Rows

        feature = featureCursor.NextRow()
0 Kudos
3 Replies
DuncanHornby
MVP Notable Contributor
David,

Just to clarify when you say "in memory layer", are you saying you have created a FeatureClass in an InMemoryWorkSpace?

FYI I knocked together some code in VBA on a FeaturelClass in the in_memory workspace which I created using the Table Select tool. My code worked fine and the only difference I can see is that you are not setting the QueryFilter. This code worked for me:

Public Sub test()
    Dim pMXDocument As IMxDocument
    Set pMXDocument = ThisDocument
    Dim pMap As IMap
    Set pMap = pMXDocument.FocusMap
    Dim pLayer As ILayer
    Set pLayer = pMap.Layer(0)
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = pLayer
    Dim pFeatureClass As IFeatureClass
    Set pFeatureClass = pFeatureLayer.FeatureClass
    Dim pTable As ITable
    Set pTable = pFeatureClass
    Dim pTableSort As ITableSort
    Set pTableSort = New TableSort
    With pTableSort
        .Ascending("Strahler") = False
        Set .Table = pTable
        Set .QueryFilter = Nothing
        .Fields = "Strahler"
        .Sort Nothing
    End With
    
    ' Get sorted cursor and write to debug window
    Dim pCursor As ICursor
    Set pCursor = pTableSort.Rows
    Dim pRow As IRow
    Set pRow = pCursor.NextRow
    Do While Not pr Is Nothing
        Debug.Print pRow.Value(pRow.Fields.FindField("Strahler"))
        Set pRow = pCursor.NextRow
    Loop
End Sub

Duncan
0 Kudos
DavidClarke1
New Contributor III
You are correct.  I am creating the layer using an "InMemoryWorkspaceFactory".

I tried setting the filter to "nothing" but get the same result.
0 Kudos
DavidClarke1
New Contributor III
I created a file-based layer and ran the same sort routine and it worked just fine.  Bug or limitation?
0 Kudos