如何按顺序对包含三个值的字典进行排序

问题描述 投票:0回答:3

所以我有一个数字和颜色值列表。

[1, Red]
[4, Yellow]
[5, Red]
[6, Yellow]
[8, White]
[9, Red]
[10, Yellow]
[13, White]
etc. etc.

字典整数最初是使用Enumerable在1到100之间的范围内随机生成的。 var lstNumbers = Enumerable .Range(1, 100).OrderBy(n => Guid.NewGuid().GetHashCode()) .ToList();

创建字典并用于将序列红色,黄色,白色,红色,黄色,白色等中的每个数字的ColorType值分配给list.var map = new Dictionary<int, ColorType>();中的所有数字。

然后我以几种不同的方式删除项目,这使我进行排序。我需要按值从这本词典中排序最终列表,白色结果位于顶部,黄色位于中间,红色位于底部。红色<黄色<白色

然后显示。

我想我可以使用一些if语句对颜色进行排序,或者为每种颜色设置参数,然后使用Orderby以某种方式按颜色值对结果进行排序。

我很难找到以这种方式对这些特定值进行排序的有效方法。

有任何想法吗?

c# sorting dictionary key-value
3个回答
1
投票

这是基于this answer的实现:

public class CustomerComparer : IComparer<KeyValuePair<int, string>>
{
    private List<string> orderedColors = new List<string>() { "White", "Yellow", "Red" };

    public int Compare(KeyValuePair<int, string> str1, KeyValuePair<int, string> str2)
    {
        return orderedColors.IndexOf(str1.Value) - orderedColors.IndexOf(str2.Value);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var unsorted = new Dictionary<int, string>()
        {
            {1, "Red"},
            {4, "Yellow"},
            {5, "Red"},
            {6, "Yellow"},
            {8, "White"},
            {9, "Red"},
            {10, "Yellow"},
            {13, "White"}
        };

        var sorted = unsorted.OrderBy(x => x, new CustomerComparer());

        foreach (var entry in sorted)
        {
            Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
        }
        Console.ReadKey();
    }
}

你可以看看this article

Update

基于对这个答案的评论,字典似乎是一个来自Enum的数字的顺序值。

更新的代码:

public class CustomerComparer : IComparer<KeyValuePair<int, ColorType>>
{
    private List<ColorType> orderedLetters = new List<ColorType>() { ColorType.White, ColorType.Yellow, ColorType.Red };

    public int Compare(KeyValuePair<int, ColorType> str1, KeyValuePair<int, ColorType> str2)
    {
        return orderedLetters.IndexOf(str1.Value) - orderedLetters.IndexOf(str2.Value);
    }
}

public enum ColorType
{
    Red,
    Yellow,
    White
}

class Program
{
    static void Main(string[] args)
    {
        var unsorted = new Dictionary<int, ColorType>()
        {
            {1, ColorType.Red},
            {4, ColorType.Yellow},
            {5, ColorType.Red},
            {6, ColorType.Yellow},
            {8, ColorType.White},
            {9, ColorType.Red},
            {10, ColorType.Yellow},
            {13, ColorType.White}
        };

        var sorted = unsorted.OrderBy(x => x, new CustomerComparer());

        foreach (var entry in sorted)
        {
            Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
        }
        Console.ReadKey();
    }
}

0
投票

这是一个提示:

Red = 100
Yellow = 110
White = 111

(只是说你想要的颜色顺序已经隐含了你提到的颜色的RGB值。所以它应该是订单价值的一个很好的来源。)


0
投票

您可以使用Enum来对符合您需求的颜色进行排序。如果identifier只是一个字符串值,则必须按字符串值查找并将其转换为int值。如果将来会有更多颜色,或者必须更改订单,只需修改枚举即可。当然需要更新,如果在枚举中找不到颜色,但不是这个问题的添加情况......

 public enum ColorCodes
{
    White = 1,
    Yellow = 2,
    Red = 3
}

class Program
{
    /// <summary>
    /// Sorts dictionary by color enum int sequence
    /// </summary>
    /// <param name="toOrder"></param>
    /// <returns></returns>
    public static Dictionary<int, string> DictionarySorter(Dictionary<int, string> toOrder)
    {
        return toOrder.OrderBy(x => (int)(ColorCodes)Enum.Parse(typeof(ColorCodes), x.Value)).ToDictionary(x => x.Key, x => x.Value);
    }


    static void Main(string[] args)
    {

        Dictionary<int, string> unsortedColors = new Dictionary<int, string>();
        unsortedColors.Add(1, "Red");
        unsortedColors.Add(4, "Yellow");
        unsortedColors.Add(5, "Red");
        unsortedColors.Add(6, "Yellow");
        unsortedColors.Add(8, "White");
        unsortedColors.Add(9, "Red");
        unsortedColors.Add(10, "Yellow");
        unsortedColors.Add(13, "White");


        var sortedDic = DictionarySorter(unsortedColors);
        foreach (var entry in sortedDic)
        {
            Console.WriteLine(string.Format("{0} - {1}", entry.Key, entry.Value));
        }
        Console.Read();
    }
}

正如其他人所指出的那样,-dictionary没有按顺序排序,所以字典排序器可以使用:

 public static List<KeyValuePair<int,string>> DictionarySorter(Dictionary<int, string> toOrder)
    {
        return toOrder.OrderBy(x => (int)(ColorCodes)Enum.Parse(typeof(ColorCodes), x.Value)).ToList();
    }
© www.soinside.com 2019 - 2024. All rights reserved.