【问题标题】:How can I sort a dictionary by an array?如何按数组对字典进行排序?
【发布时间】:2020-11-02 11:16:09
【问题描述】:

我使用 itextsharp 获取 PDF 元数据,如下所示:

var pdf = new PdfReader(__PDF_PATH__);
var main = new[] { "Title", "Subject", "Creator", "Author", "Producer", "Keywords", "ModDate", "CreationDate" };
var metadatas = pdf.Info; // returns Dictionary<string, string> metadatas

字典的键在main 数组中。我想根据给定的main 数组对字典的键进行排序。但我没有做到。

【问题讨论】:

标签: c# sorting dictionary itext


【解决方案1】:

您可以在此处查看此答案: Sorting a Dictionary in place with respect to keys

或者您可以在此处查看此方法: https://www.dotnetperls.com/sort-dictionary

但基本上你不能在字典中做到这一点。您可以转换为其他数据结构。

【讨论】:

    【解决方案2】:

    正如其他人所提到的,Dictionary 没有顺序,您无法对其进行排序。

    根据您想对metadatas 字典执行的操作,您可以使用main 数组将数据复制到不同的数据结构中,例如SortedDictionarySortedList 或@ 987654326@.

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      相关资源
      最近更新 更多