Sort unique value symbology classes using values from a second field.


This article pertains to ArcGIS versions 8.x and 9.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

This article describes how to sort unique value symbology classes using values from a second field.

When using the Layer Properties > Symbology > Categories > Match to Symbols Style method to symbolize, you can sort alphabetically on the Value Field or manually using the Up and Down arrow buttons. If you don't want alphabetic sorting, or if you have many different classes, you can sort using a second field and the following ArcObjects code.

Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward


  1. Add a numeric item to your attribute table and populate it with values indicating the order the symbols should be listed in the Table of Contents and the legend.
  2. Right-click the layer in the Table of Contents and choose Properties.
  3. Click the Symbology tab and choose Categories > Unique values, many fields.
  4. For the first value field, choose the numeric sort field which was added. For the second value field choose your symbology field.
  5. Click the Add All Values button and click OK.
  6. The class labels will have values from both value fields and will look something like the following in the TOC and legend.
[O-Image] How the legend originally looks
  1. Run the following code to remove the leading value, comma, and space from the legend display.

    How do I run this code?
    1. Click Tools > Macros > Visual Basic Editor.
    2. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument then right-click and select View Code.
    3. Copy the code below into the code module.
    4. Close the Visual Basic Editor.
    5. Run the code.
Option Explicit

Sub TrimClassLabels()
    Dim pMxDoc As IMxDocument
    Dim pGeoFeatLayer As IGeoFeatureLayer
    Set pMxDoc = Application.Document
    ' change this index to operate on another layer
    Set pGeoFeatLayer = pMxDoc.ActiveView.FocusMap.Layer(0)     
    Dim pUVRend As IUniqueValueRenderer
    Set pUVRend = pGeoFeatLayer.Renderer
    Dim i As Integer
    Dim j As Integer
    Dim strLabel As String
    Dim strValue As String
    For i = 0 To pUVRend.ValueCount - 1
        strValue = pUVRend.Value(i)
        j = InStr(strValue, ",")
        strLabel = Right(strValue, Len(strValue) - j)
        ' get rid of leading space for 2nd value
        strLabel = LTrim(strLabel)
        pUVRend.Label(strValue) = strLabel
    Next i
End Sub
  1. The result will look similar to the following image.
[O-Image] How the legend looks now

Article ID:000006167

  • ArcMap 8 x
  • ArcMap 9 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic