using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
public static class Program
static int totalNumOfChangeNotifications = 0;
static ObservableCollection<Person> observable;
public static void Main()
Console.WriteLine("\nsorting items...");
Console.WriteLine("AndrewSort: " + totalNumOfChangeNotifications);
observable.AndrewMoveSort();
Console.WriteLine("AndrewMoveSort: " + totalNumOfChangeNotifications);
observable.TopVotedSort();
Console.WriteLine("TopVotedSort: " + totalNumOfChangeNotifications);
Console.WriteLine("MySort: " + totalNumOfChangeNotifications);
totalNumOfChangeNotifications = 0;
observable = new ObservableCollection<Person>
new Person(){ Id = 0, Name = "A" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 2, Name = "B" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 1, Name = "A" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 2, Name = "A" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "C" },
new Person(){ Id = 3, Name = "A" },
observable.CollectionChanged += CollectionChangedEventHandler;
static void CollectionChangedEventHandler(object sender, NotifyCollectionChangedEventArgs e)
totalNumOfChangeNotifications++;
Console.WriteLine("Type of change: " + e.Action);
public static void AndrewSort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
List<T> sorted = collection.OrderBy(x => x).ToList();
while (ptr < sorted.Count)
if (!collection[ptr].Equals(sorted[ptr]))
collection.RemoveAt(ptr);
collection.Insert(sorted.IndexOf(t), t);
public static void AndrewMoveSort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
List<T> sorted = collection.OrderBy(x => x).ToList();
while (ptr < sorted.Count)
if (!collection[ptr].Equals(sorted[ptr]))
collection.Move(ptr, sorted.IndexOf(t));
public static void TopVotedSort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
List<T> sorted = collection.OrderBy(x => x).ToList();
for (int i = 0; i < sorted.Count(); i++)
collection.Move(collection.IndexOf(sorted[i]), i);
public static void MySort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
foreach (T item in collection.OrderBy(x => x))
int oldIndex = collection.IndexOf(item);
if (oldIndex != newIndex)
collection.Move(oldIndex, newIndex);
public class Person : IComparable<Person>, IEquatable<Person>
public string Name { get; set; }
public int Id { get; set; }
public int Age { get; set; }
public int CompareTo(Person other)
return this.Name.CompareTo(other.Name);
public override string ToString()
return Name + " aged " + Age + " and Id = " + Id;
public bool Equals(Person other)
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name;
public override bool Equals(object obj)
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Person)obj);
public override int GetHashCode()
return (Name != null ? Name.GetHashCode() : 0);