Jump to content
  • How to set Custom Sort order on a given field (Data Column) using using Spotfire® C# API


    It is possible to re-order the values in a Data Column using the API DataColumnProperties.SetCustomSortOrder Method. Sample tool code to do this is included below.

    Introduction

    It is possible to re-order the values in a Data Column using the API DataColumnProperties.SetCustomSortOrder Method.

    Sample tool code to do this is included below:

    Code Sample

    // Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license.
    // Pick the first string column.
    DataColumn dataColumn = null;
    foreach (var column in application.Document.Data.Tables.DefaultTableReference.Columns)
    {
        if (column.Properties.DataType == DataType.String)
        {
            dataColumn = column;
            break;
        }
    }
    // Get the distinct values in a column.
    DataNodeCollection nodes;
    if (!dataColumn.Hierarchy.Levels.LeafLevel.TryGetNodes(500, out nodes))
    {
        // Probably too many nodes.
        return;
    }
    
    List values = new List();
    foreach (var node in nodes)
    {
        var dataValue = node.Value;
        if (dataValue.IsNullValue)
        {
            continue;
        }
    
        values.Add((string)dataValue.ValidValue);
    }
    
    // Shuffle the values.
    Random random = new Random();
    for (int i = 0; i < values.Count; ++i)
    {
        int j = random.Next(i, values.Count);
        var temp = values[j];
        values[j] = values[i];
        values[i] = temp;
    }
    
    dataColumn.Properties.SetCustomSortOrder(values);
    
    MessageBox.Show("New custom sort order assigned for column: " + dataColumn.Properties.Name);
     

    References

    License:  TIBCO BSD-Style License

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...