English

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

Summary

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.

Procedure

  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 your TOC and legend.
    [O-Image] How the legend originally looks

  7. Run the following code to remove the leading value, comma, and space from the legend display.

    How do I run this code?

    A) Click Tools > Macros > Visual Basic Editor.

    B) In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument then right-click and select View Code.

    C) Copy the code into the code module.

    D) Close the Visual Basic Editor.

    E) Run the code.


    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

    pMxDoc.UpdateContents
    pMxDoc.ActiveView.Refresh

    End Sub

  8. The result will look similar to the following image.
    [O-Image] How the legend looks now