Skip to main content

How to: Display Qualitative Scale Values Sorted in a Custom Sort Order

  • 2 minutes to read

Assign an object of a class that implements the IComparer interface to the AxisBase.QualitativeScaleComparer property.

using System;
using System.Collections;

namespace QualitativeScaleCustomSortOrderSample {
    class NumberComparer : IComparer {
        public int Compare(object x, object y) {
            int iX = NumberConverter.ToInt(x);
            int iY = NumberConverter.ToInt(y);
            return iX - iY;
        }
    }

    class NumberConverter {
        public static int ToInt(object o) {
            string stringNumber = o as string;
            int number = 0;
            if (stringNumber == null) return -1;
            if (Int32.TryParse(stringNumber, out number))
                return number;
            switch (stringNumber.ToLower()) {
                case "one": return 1;
                case "two": return 2;
                case "three": return 3;
            }
            return number;
        }
    }
}