Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:00:16+00:00 2026-05-13T10:00:16+00:00

I have a following class : [DataContract] public class Pair<TKey, TValue> : INotifyPropertyChanged, IDisposable

  • 0

I have a following class :

[DataContract]
public class Pair<TKey, TValue> : INotifyPropertyChanged, IDisposable
{
    public Pair(TKey key, TValue value)
    {
        Key = key;
        Value = value;
    }

    #region Properties
    [DataMember]
    public TKey Key
    {
        get
        { return m_key; }
        set
        {
            m_key = value;
            OnPropertyChanged("Key");
        }
    }
    [DataMember]
    public TValue Value
    {
        get { return m_value; }
        set
        {
            m_value = value;
            OnPropertyChanged("Value");
        }
    }
    #endregion

    #region Fields
    private TKey m_key;
    private TValue m_value;
    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    { }

    #endregion
}

Which I’ve put in an ObservableCollection :

ObservableCollection<Pair<ushort, string>> my_collection = 
    new ObservableCollection<Pair<ushort, string>>();

my_collection.Add(new Pair(7, "aaa"));
my_collection.Add(new Pair(3, "xey"));
my_collection.Add(new Pair(6, "fty"));

Q : How do I sort it by key ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:00:16+00:00Added an answer on May 13, 2026 at 10:00 am

    Sorting an observable and returning the same object sorted can be done using an extension method. For larger collections watch out for the number of collection changed notifications.

    I have updated my code to improve performance (thanks to nawfal) and to handle duplicates which no other answers here do at time of writing. The observable is partitioned into a left sorted half and a right unsorted half, where each time the minimum item (as found in the sorted list) is shifted to the end of the sorted partition from the unsorted. Worst case O(n). Essentially a selection sort (See below for output).

    public static void Sort<T>(this ObservableCollection<T> collection)
            where T : IComparable<T>, IEquatable<T>
        {
            List<T> sorted = collection.OrderBy(x => x).ToList();
    
            int ptr = 0;
            while (ptr < sorted.Count - 1)
            {
                if (!collection[ptr].Equals(sorted[ptr]))
                {
                    int idx = search(collection, ptr+1, sorted[ptr]);
                    collection.Move(idx, ptr);
                }
                
                ptr++;
            }
        }
    
        public static int search<T>(ObservableCollection<T> collection, int startIndex, T other)
                {
                    for (int i = startIndex; i < collection.Count; i++)
                    {
                        if (other.Equals(collection[i]))
                            return i;
                    }
        
                    return -1; // decide how to handle error case
                }
    

    usage:
    Sample with an observer (used a Person class to keep it simple)

        public class Person:IComparable<Person>,IEquatable<Person>
                { 
                    public string Name { get; set; }
                    public int Age { get; set; }
        
                    public int CompareTo(Person other)
                    {
                        if (this.Age == other.Age) return 0;
                        return this.Age.CompareTo(other.Age);
                    }
        
                    public override string ToString()
                    {
                        return Name + " aged " + Age;
                    }
        
                    public bool Equals(Person other)
                    {
                        if (this.Name.Equals(other.Name) && this.Age.Equals(other.Age)) return true;
                        return false;
                    }
                }
        
              static void Main(string[] args)
                {
                    Console.WriteLine("adding items...");
                    var observable = new ObservableCollection<Person>()
                    {
                        new Person {Name = "Katy", Age = 51},
                        new Person {Name = "Jack", Age = 12},
                        new Person {Name = "Bob", Age = 13},
                        new Person {Name = "Alice", Age = 39},
                        new Person {Name = "John", Age = 14},
                        new Person {Name = "Mary", Age = 41},
                        new Person {Name = "Jane", Age = 20},
                        new Person {Name = "Jim", Age = 39},
                        new Person {Name = "Sue", Age = 5},
                        new Person {Name = "Kim", Age = 19}
                    };
        
                    //what do observers see?
                
        
    observable.CollectionChanged += (sender, e) =>
            {
                Console.WriteLine(
                    e.OldItems[0] + " move from " + e.OldStartingIndex + " to " + e.NewStartingIndex);
                int i = 0;
                foreach (var person in sender as ObservableCollection<Person>)
                {
                    if (i == e.NewStartingIndex)
                    {
                        Console.Write("(" + (person as Person).Age + "),");
                    }
                    else
                    {
                        Console.Write((person as Person).Age + ",");
                    }
                    
                    i++;
                }
    
                Console.WriteLine();
            };
    

    Details of sorting progress showing how the collection is pivoted:

    Sue aged 5 move from 8 to 0
    (5),51,12,13,39,14,41,20,39,19,
    Jack aged 12 move from 2 to 1
    5,(12),51,13,39,14,41,20,39,19,
    Bob aged 13 move from 3 to 2
    5,12,(13),51,39,14,41,20,39,19,
    John aged 14 move from 5 to 3
    5,12,13,(14),51,39,41,20,39,19,
    Kim aged 19 move from 9 to 4
    5,12,13,14,(19),51,39,41,20,39,
    Jane aged 20 move from 8 to 5
    5,12,13,14,19,(20),51,39,41,39,
    Alice aged 39 move from 7 to 6
    5,12,13,14,19,20,(39),51,41,39,
    Jim aged 39 move from 9 to 7
    5,12,13,14,19,20,39,(39),51,41,
    Mary aged 41 move from 9 to 8
    5,12,13,14,19,20,39,39,(41),51,
    

    The Person class implements both IComparable and IEquatable the latter is used to minimise the changes to the collection so as to reduce the number of change notifications raised

    • EDIT Sorts same collection without creating a new copy *

    To return an ObservableCollection, call .ToObservableCollection on *sortedOC* using e.g. [this implementation][1].

    **** orig answer – this creates a new collection ****
    You can use linq as the doSort method below illustrates. A quick code snippet: produces

    3:xey
    6:fty
    7:aaa

    Alternatively you could use an extension method on the collection itself

    var sortedOC = _collection.OrderBy(i => i.Key);
    
    private void doSort()
    {
        ObservableCollection<Pair<ushort, string>> _collection = 
            new ObservableCollection<Pair<ushort, string>>();
    
        _collection.Add(new Pair<ushort,string>(7,"aaa"));
        _collection.Add(new Pair<ushort, string>(3, "xey"));
        _collection.Add(new Pair<ushort, string>(6, "fty"));
    
        var sortedOC = from item in _collection
                       orderby item.Key
                       select item;
    
        foreach (var i in sortedOC)
        {
            Debug.WriteLine(i);
        }
    
    }
    
    public class Pair<TKey, TValue>
    {
        private TKey _key;
    
        public TKey Key
        {
            get { return _key; }
            set { _key = value; }
        }
        private TValue _value;
    
        public TValue Value
        {
            get { return _value; }
            set { _value = value; }
        }
        
        public Pair(TKey key, TValue value)
        {
            _key = key;
            _value = value;
    
        }
    
        public override string ToString()
        {
            return this.Key + ":" + this.Value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
I have the following DTO definition:- [DataContract] public class AddProductDTO { [DataMember] public string
I have the following: [DataContract] public class Foo { [DataMember(EmitDefaultValue = true) public bool
Basically if I have the following: [DataContract] public class Foo { [MyCustomAttribute(...)] [DataMember(IsRequired =
I have the following class: [DataContract] public class FileUploaderResult { [DataMember] public bool Success
I have the following code: [DataContract(Namespace = )] public class User { [DataMember] public
I have the following two models [DataContract] public class Event { [Key] public int
I have the following class [DataContract(Namespace = , Name = VersionRange)] public sealed class
I have the following WCF DataContract: [DataContract] public class Occupant { private string _Name;
I have following class (as seen through reflector) public class W : IDisposable {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.