当Key等于订单号时,如何在字典中移动项

本文关键字:字典 移动 Key 订单号 | 更新日期: 2023-09-27 18:05:26

我有一个像这样的字典:

var newDictionary = new Dictionary<int, string>();

此字典中的int (key)是项的序号。

假设我有一个这样的列表

{1, "item1"}
{2, "item2"}
{3, "item3"}
{4, "item4"}

在我的控制器中,我传递了旧的商品订单号和新的订单号。因此,例如,如果我想将"item3"移动为第一项,我将传递给控制器3(旧订单号)和1(新订单号)。所以我的列表看起来像这样:

{1, "item3"}
{2, "item1"}
{3, "item2"}
{4, "item4"}

当Key等于订单号时,如何在字典中移动项

这不是您通常使用字典的类型。您可以考虑使用List而不是Dictionary,并在列表上使用RemoveAt和InsertAt方法。如果您的列表非常大,您可能需要考虑另一种方法(出于性能原因),但对于少量数据,它应该工作得很好。

这样看:

你不改变项的顺序,但改变一个的关键点。在字典中,项的有效顺序并不重要,重要的是指向值的

您想要的是下次有人请求newDictionary[1]时将收到"Item3",就像内容一样。

的例子:

//swap 
string val1 = d[1]; 
string val3 = d[3]; 
d[1] = val3; 
d[3] = val1;

如果你需要,顺便说一下,一些特定的顺序,有SortedDictionary。

好的,感谢所有的答案,但不幸的是,大多数建议不会为我想要的工作。然而,这就是我如何做到这一点:

public void ReorderDictionary(int oldIndex, int newIndex)
    {
        var oldDictionary = new Dictionary<int, string>();
        oldDictionary.Add(1, "item1");
        oldDictionary.Add(2, "item2");
        oldDictionary.Add(3, "item3");
        oldDictionary.Add(4, "item4");
        var movedItem = oldDictionary[oldIndex];
        oldDictionary.Remove(oldIndex);
        var newDictionary = new Dictionary<int, string>();
        var offset = 0;
        for (var i = 0; i < oldDictionary.Count; i++)
        {
            if (i + 1 == newIndex)
            {
                newDictionary.Add(i, movedItem);
                offset = 1;
            }
            var oldEvent = oldDictionary[oldDictionary.Keys.ElementAt(i)];
            newDictionary.Add(i + offset, oldEvent);
        }
        if (newIndex > oldDictionary.Count)
        {
            newDictionary.Add(oldDictionary.Count + 1, movedItem);
        }
    }

可能不是最好的方法,但不幸的是我不得不使用一个过时的系统。但至少这是可行的:D

我在控制台应用程序中尝试了一些东西。希望这能给你你正在寻找的:

public class Program
{
    static void Main(string[] args)
    {
        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        dictionary.Add(1, "item1");
        dictionary.Add(2, "item2");
        dictionary.Add(3, "item3");
        dictionary.Add(4, "item4");
        Dictionary<int, string> sortedDic = ResortDictionary(dictionary, 1, 4);
        foreach (KeyValuePair<int, string> row in sortedDic)
        {
            Console.WriteLine("Key: " + row.Key + "  Value: " + row.Value);
        }
        Console.ReadLine();
    }
    public static Dictionary<int, string> ResortDictionary(Dictionary<int, string> dictionary, int oldOrderNumber, int newOrderNumber)
    {
        string oldOrderNumberValue = dictionary[oldOrderNumber];
        string newOrderNumberValue = dictionary[newOrderNumber];
        dictionary[newOrderNumber] = oldOrderNumberValue;
        dictionary[oldOrderNumber] = newOrderNumberValue;
        return dictionary;
    }
}

如果您想让它更短一点,请在您的重新排序方法中使用foreach:

private static Dictionary<int, string> ReorderDictionary(Dictionary<int, string> originalDictionary, int newTopItem)
{
    // Initialize ordered dictionary with new top item.
    var reorderedDictionary = new Dictionary<int, string> {{ newTopItem, originalDictionary[newTopItem] }};
    foreach (var item in originalDictionary)
    {
        if (item.Key == newTopItem)
        {
            // Skip the new top item.
            continue;
        }
        reorderedDictionary.Add(item.Key, item.Value);
    }
    return reorderedDictionary;
}