How can I sort a dictionary by an array?

Viewed 75

I get the PDF metadatas using itextsharp as follows:

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

The keys of the dictionary are as in the main array. I want to sort the dictionary's keys according to the given main array. But I did not manage.

2 Answers

As other folks have mentioned, a Dictionary has no order, and you can't sort it.

Depending on what you want to do with your metadatas dictionary, you could use your main array to copy the data into a different data structure, such as a SortedDictionary, a SortedList, or a List<List<string>.

Related