I have an Array list of a custom class which is essentially has two attributes Key and Value
public class myClass
{
public key { get; set; }
public value { get; set; }
}
ArrayList myList = new ArrayList();
// myList is a list of myClass
How can I sort myList by Key?
I’ve been doing this below in the past the problem is i can’t use it anymore because it doesn’t handle the case of duplicate keys, i cant have two of same keys in dictionary.
// pseudo code
loop and add to dictionary (Key, Value)
sort dictionary
Dictionary<string, string> sortedTypes = myList.
OrderBy(i => i.Key).ToDictionary(i => i.Key, i => i.Value);
edit: I need it sorted by key first then value as second criteria
First of all: Don’t use
ArrayList. Use the generic typeList<myClass>instead.Then, just use the
List.Sortmethod, passing an appropriate delegate to use for the comparison.