如何更改一些字典字段的位置后,我排序的值按字母顺序

本文关键字:排序 顺序 何更改 字典 位置 字段 | 更新日期: 2023-09-27 18:18:52

我有一个Enum AllTypes

public enum AllTypes
    {
        [Description("Best")]
         best = 0,
        [Description("Adv")]
         adv = 1,
        [Description("Cas")]
         cas = 21,
        [Description("Cas2")]
        cas2 = 3,
        [Description("Cat")]
        cat = 4,
        [Description("E-test")]
        e-test = 5,
        [Description("Edit")]
        edit = 6,
        [Description("Ev")]
        ev = 7,
        [Description("Git")]
        git = 8,
        [Description("Ga")]
        ga = 9,
        [Description("Zb")]
        zb = 10
        [Description("Test")]
        test = 20
    }

对于按字母顺序提取这个Enum,我调用这个方法:

public static IDictionary<int, string> GetAllTypes<TEnum>() where TEnum : struct
    {
        var enumerationType = typeof(TEnum);
        if (!enumerationType.IsEnum)
            throw new ArgumentException("Enumeration type is expected.");
        var dictionary = new Dictionary<int, string>();
        foreach (int value in Enum.GetValues(enumerationType))
        {
            var name = GetDescription((MyEnums.AllTypes)value);
            dictionary.Add(value, name);
        }
        var sorted = from dictionar in dictionary
                     orderby dictionar.Value ascending
                     select dictionar;
        var dict = new Dictionary<int, string>();
        foreach (var item in sorted)
        {
            dict.Add(item.Key, item.Value);
        }
        return dict;
    }

现在我想改变最佳类型的位置,把它放在字典的第一个位置,如果我按键删除它,然后在字典中添加它,它被放在第三个位置,有人建议吗?

如何更改一些字典字段的位置后,我排序的值按字母顺序

正如其他人所指出的,您必须使用SortedDictionary类。

但是,你需要做更多。

1)您希望Enum的一个特殊值是第一个

2)你想按描述排序。SortedDictionary按键而不是按值排序。

我建议这样做:

定义一个特殊的比较器类,允许我们像这样指定一个特殊的比较函数:

public class CustomComparer<T> : IComparer<T>
{
    private readonly Func<T, T, int> m_Comparer;
    public CustomComparer(Func<T, T, int> comparer)
    {
        m_Comparer = comparer;
    }
    public int Compare(T x, T y)
    {
        return m_Comparer(x, y);
    }
}

然后使用SortedDictionary与这个比较器:

public static IDictionary<int, string> GetAllTypes<TEnum>(TEnum special_value) where TEnum : struct
{
    var enumerationType = typeof(TEnum);
    if (!enumerationType.IsEnum)
        throw new ArgumentException("Enumeration type is expected.");
    int special_value_int = Convert.ToInt32(special_value);
    var dictionary = new SortedDictionary<int, string>(
        new CustomComparer<int>((x, y) =>
        {
            if (x == special_value_int && x != y)
                return -1;
            if (y == special_value_int && x != y)
                return 1;
            var description_x = GetDescription((AllTypes) x);
            var description_y = GetDescription((AllTypes)y);
            return description_x.CompareTo(description_y);
        }));
    foreach (int value in Enum.GetValues(enumerationType))
    {
        var name = GetDescription((AllTypes)value);
        dictionary.Add(value, name);
    }
    return dictionary;
}

请注意我是如何定义一个特殊函数的,它将特殊值视为较小的值,并使用枚举的描述进行比较。

你可以这样使用它:

var result = GetAllTypes<AllTypes>(AllTypes.best);

这里我指定了应该在开始处的特殊值

字典不是排序的,所以您最好使用List<KeyValuePair>SortedDictionary