0

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

6 Answers 6

6

First of all: Don't use ArrayList. Use the generic type List<myClass> instead.

Then, just use the List.Sort method, passing an appropriate delegate to use for the comparison.

list.Sort((x, y)=>x.key.CompareTo(y.key))
Sign up to request clarification or add additional context in comments.

Comments

3

If myList were a List<myClass> instead, then you could just do:

myList = myList.OrderBy(x => x.key).ToList();

You could also use the Sort method on List itself, but that requires some awkward hoops to use the comparator.

Incidentally, class names and public fields/properties should be capitalized (MyClass and Key respectively.)

Comments

2

Why aren't you using a Dictionary<key, value>? it lets you just grab anything by the key. You can also access the keys independently via methods of the class as well. If you use a built in type like string, the sorting comparator is already there for you.

edit: the above is wrong for this situation. but I am leaving it to show that Dictionary does not work here.

Another suggestion: you could use something like SortedList<Key, List<Value>> so your list will always be sorted. The only caveat is that insertion time is greater than that of a regular list.

2 Comments

The OP stated 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.
aha. i'm blind. Well a "dictionary" by normal definition doesn't allow duplicate keys. He is looking more for something like a hash map, but with a list at each location.
2

If you are required to start with a List, you can easily convert to a dictionary and then sort by value:

objectList.OrderBy(i => i.Value).ToDictionary(i => i.Key, i => i.Value)

Comments

1

You could convert the array to List with Array.AsList(array) and the use linq with something like

list.OrderBy(x=> x.Key)

:)

Comments

1

Here an example:

 public class EventAlarm
{
    List<EventProperty> propertyList = null;

    public EventAlarm()
    {
       propertyList = new List<EventProperty>();
    }

    public addProperty(string key, string value)
    {
        propertyList.Add(new EventProperty(key, value));
    }

    public sortProperty()
    {
        propertyList.Sort((x, y)=>x.key.CompareTo(y.key)) 
    }

}


private class EventProperty
{
    #region  Properties

    private string key;
    private string value;
    private bool isPropertyValid;

    public string Key
    {
        get { return key; }
        set { key = value; }
    }

    public string Value
    {
      get { return this.value; }
      set { this.value = value; }
    }

    public bool IsPropertyValid
    {
      get { return isPropertyValid; }
      set { isPropertyValid = value; }
    }

    #endregion  Properties

    #region Constructor

    public EventProperty(string key, string value)
    {
        this.Key = key;
        this.Value = Value;
    }

    #endregion Constructor

    #region Methods

    public void PropertyValidation()
    {
        // Here write Code ...
    }

    #endregion Methods
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.