public static SortedSet<T> ToSortedSet<T>(this IEnumerable<T> t) { SortedSet<T> retval = new SortedSet<T>(); t.ToList().ForEach(x => retval.Add(x)); return retval; }And here's how to use it:
static void Main(string[] args) { // Create some customer objects and add them to a SortedSet. Customer cust1 = new Customer { CustomerName = "Wal Mart", CreditBalance = 525565.55M}; Customer cust2 = new Customer { CustomerName = "Ziggy's"}; Customer cust3 = new Customer { CustomerName = "Bill's Place", CreditBalance = 2545.18M }; SortedSet<Customer> customers = new SortedSet<Customer>(); customers.Add(cust1); customers.Add(cust2); customers.Add(cust3); // Query the sorted set using Linq var custs = (from c in customers select c).ToSortedSet(); // Add an item to the result set. You will see that it appears first in the list custs.Add(new Customer { CustomerName = "Able's Axel Shop" }); // Show the results foreach (Customer customer in custs) Console.WriteLine(customer.CustomerName); Console.ReadLine(); }And finally, here's the Customer class. In the CompareTo you can decide
public class Customer : IComparable { public string CustomerName { get; set; } public DateTime? DateAdded { get; set; } public decimal? CreditBalance { get; set; } public int CompareTo(object obj) { if (obj is Customer) { Customer c = (Customer)obj; return CustomerName.CompareTo(c.CustomerName); //return CustomerName.CompareTo(c.CreditBalance.ToString()); } else throw new ArgumentException("Object is not a Customer."); } }