How To: Sort unique value symbology classes using values from a second field.
Note: 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.
Note: 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
- 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.
- Right-click the layer in the Table of Contents and choose Properties.
- Click the Symbology tab and choose Categories > Unique values, many fields.
- For the first value field, choose the numeric sort field which was added. For the second value field choose your symbology field.
- Click the Add All Values button and click OK.
- The class labels will have values from both value fields and will look something like the following in the TOC and legend.
- Run the following code to remove the leading value, comma, and space from the legend display.
How do I run this code?
- Click Tools > Macros > Visual Basic Editor.
- In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument then right-click and select View Code.
- Copy the code below into the code module.
- Close the Visual Basic Editor.
- 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 pMxDoc.UpdateContents pMxDoc.ActiveView.Refresh End Sub
- The result will look similar to the following image.