Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Sort Dictionary Array

I have Following Array an want to sort it by the time key:

Dictionary<string, object>[] callHistory = CharactersPhone.CharactersPhoneCallHistorys_.ToList().Where(x => x.charPhoneNumber == Characters.GetCharacterPhonenumber(charId)).Select(x => 
                new Dictionary<string, object>
                {
                    {"charPhoneNumber" , x.charPhoneNumber.ToString()},
                    {"targetPhoneNumber" , x.targetNumber.ToString()},
                    {"state" , x.state},
                    {"outgoingstate", x.outgoingState},
                    {"time" , x.timestamp.ToString("dd.MM.yyyy HH:mm")}
                }).ToArray();

I try it with the following:

var sorted = callHistory.OrderBy(x => Array.IndexOf(callHistory, x.time))

but the key "x.time" is not found, i think i have a little brain lag here or something is fundamental wrong but i doesnt find the answer. Maybe someone here can help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re trying to access a keyed value as though it were a property or field.
Instead of accessing it with the . operator, use the index operator, as shown below:

var a = new Dictionary<string, object>
                {
                    {"charPhoneNumber" , 'a'},
                    {"targetPhoneNumber" , 'b'},
                    {"state" , 'c'},
                    {"outgoingstate", 'd'},
                    {"time" , 'e'}
                };
var b = a["time"];
Console.WriteLine(b); // Outputs "e"

In your case, you’d use x["time"] in place of x.time.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading