How to sort a field in a dbf table in descending order?

1999
3
01-13-2011 10:00 PM
DemetrisDemetriou
New Contributor
I noticed that the ItableSort interface has only an ascending property. How can I sort a table based on a field but in descending order? I work with VBA and ArcObjects.

Thanks
Deme
0 Kudos
3 Replies
DuncanHornby
MVP Notable Contributor
Demetris,

See sample VBA code below on how to get a descending sort order.

Duncan

Public Sub Test()
    Dim pTable As ITable
    Set pTable = FindTableInMap("Downstream_Polylines")
    Dim pTableSort As ITableSort
    Set pTableSort = New esriGeoDatabase.TableSort
    With pTableSort
        Set .Table = pTable
        Set .QueryFilter = Nothing
        .Fields = "OBJECTID"
        .Ascending("OBJECTID") = True ' TRUE is ascending, if set to FALSE then descending
        .Sort Nothing
    End With
    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(0)
        Set pRow = pCursor.NextRow
    Loop
End Sub
0 Kudos
DemetrisDemetriou
New Contributor
Dear Duncan,
I adjusted your the VBA's sample in my code as shown below but nothing works either with True or False. Could you understand why? Thanks
Demetris

Public Sub Test6()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

'Get the active map (data frame)
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap

Dim pTableCollection As ITableCollection
Set pTableCollection = pMap

'Find the "Rankingtable"
Dim i As Integer
Dim pDataSet As IDataset
Dim pRankingTable As ITable

Dim blnFoundTable As Boolean
blnFoundTable = False

For i = 0 To pTableCollection.TableCount - 1
Set pRankingTable = pTableCollection.Table(i)

Set pDataSet = pRankingTable 'QI

If pDataSet.Name = "RankingTable" Then
blnFoundTable = True
Exit For
End If

Next i

If blnFoundTable = False Then
MsgBox "Table not loaded in the data frame"
Exit Sub
End If


Dim pTable As ITable
Set pTable = pRankingTable
Dim pTableSort As ITableSort
Set pTableSort = New esriGeoDatabase.TableSort
With pTableSort
Set .Table = pRankingTable
Set .QueryFilter = Nothing
.Fields = "R_Order"
.Ascending("R_Order") = False ' TRUE is ascending, if set to FALSE then descending
.Sort Nothing
End With


End Sub


Demetris, 

See sample VBA code below on how to get a descending sort order. 

Duncan 

Public Sub Test()
    Dim pTable As ITable
    Set pTable = FindTableInMap("Downstream_Polylines")
    Dim pTableSort As ITableSort
    Set pTableSort = New esriGeoDatabase.TableSort
    With pTableSort
        Set .Table = pTable
        Set .QueryFilter = Nothing
        .Fields = "OBJECTID"
        .Ascending("OBJECTID") = True ' TRUE is ascending, if set to FALSE then descending
        .Sort Nothing
    End With
    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(0)
        Set pRow = pCursor.NextRow
    Loop
End Sub
0 Kudos
DuncanHornby
MVP Notable Contributor
Demetris,

I just tried you code on a table using the numeric field ObjectID and it works fine for me, this suggests there is something wrong with you R_Order field, does it contain <Null> values for example? Can you sort by this field in ArcMap?

Duncan
0 Kudos