public OrderedList(IDictionary <TKey, TValue> dictionary, bool sorted = false) { _sorted = sorted; if (dictionary == null) { dictionary = new Dictionary <TKey, TValue>(); } if (_sorted) { _internalList = new SortedList <TKey, TValue>(dictionary); } else { _order = new Dictionary <TKey, int>(); _comparer = new OrderComparer(ref _order); _internalList = new SortedList <TKey, TValue>(_comparer); // keep prder of the IDictionary foreach (var kvp in dictionary) { Add(kvp); } } }
static void Main() { List <Bar> listToOrder = new List <Bar>(); listToOrder.Add(new Bar() { Id = 5, Keywords = "Hello", Organization = "Arlando" }); listToOrder.Add(new Bar() { Id = 12, Keywords = "Table", Organization = "Fuelta", Name = "Deep" }); listToOrder.Add(new Bar() { Id = 12, Keywords = "Table", Organization = "Fuelta", Name = "Inherit" }); listToOrder.Add(new Bar() { Id = 1, Keywords = "Muppet", Organization = "Coke" }); listToOrder.Add(new Bar() { Id = 6, Keywords = "Grumpy", Organization = "Snow" }); listToOrder.Add(new Bar() { Id = 9, Keywords = "Johny", Organization = "White" }); listToOrder.Add(new Bar() { Id = 12, Keywords = "Table", Organization = "Bruno" }); listToOrder.Add(new Bar() { Id = 12, Keywords = "Table", Organization = "Fuelta" }); listToOrder.Add(new Bar() { Id = 7, Keywords = "Set", Organization = "Voltra" }); listToOrder.Add(new Bar() { Id = 45, Keywords = "Brr", Organization = "Elvis" }); listToOrder.Add(new Bar() { Id = 15, Keywords = "Tsss", Organization = "Marion" }); OrderComparer <Bar> myOrder = new OrderComparer <Bar>(); listToOrder.Sort(myOrder); foreach (Bar oneBar in listToOrder) { Console.WriteLine(oneBar.Id + " " + oneBar.Keywords + " " + oneBar.Organization); } Console.ReadKey(); }
public void Check_That_Order_Comparer_Returns_False_For_Not_Equal_Orders() { var order1 = new Order { NorthwindId = 1, IsReadonly = true, OrderItems = new List <OrderItem>(), OrderDate = new DateTime(), SessionKey = "key", OrderStatus = OrderStatus.New, OrderId = 5, }; var order2 = new Order { NorthwindId = 4, IsReadonly = true, OrderItems = new List <OrderItem>(), OrderDate = new DateTime(), SessionKey = "key", OrderStatus = OrderStatus.New, OrderId = 5, }; var comparer = new OrderComparer(); bool result = comparer.AreEqual(order1, order2); Assert.IsTrue(!result); }
public void Check_That_Order_Comparer_Throws_Exception_When_Obj2_Has_Wrong_Type() { var comparer = new OrderComparer(); var obj1 = new Order(); var obj2 = new Object(); comparer.AreEqual(obj1, obj2); }
public static BinaryTreeNode <T> ConstructTree(List <T> preorder, List <T> inorder) { var comparer = new OrderComparer <T>(inorder); var root = new BinaryTreeNode <T>(preorder[0]); for (int i = 1; i < preorder.Count; i++) { root.InsertValue(preorder[i], comparer); } return(root); }
public void TestAskComparer() { var askComparer = OrderComparer.DescAskComparer(); var o1 = new ExchangeOrder(1, 1, OrderSide.Sell, OrderType.Limit, 10, 10); var o2 = new ExchangeOrder(2, 1, OrderSide.Sell, OrderType.Limit, 9, 10); var c1 = askComparer.Compare(o1, o2); Assert.AreEqual(1, c1, string.Format("AskComparer: lower price should win over higher price. {0} vs {1}", o1.Price, o2.Price)); var c2 = askComparer.Compare(o2, o1); Assert.AreEqual(-1, c2, "AskComparer: price comparison not symmetric"); Thread.Sleep(TimeSpan.FromTicks(10000)); //otherwhise timestamp comparison is not precise enough var o3 = new ExchangeOrder(3, 1, OrderSide.Sell, OrderType.Limit, 10, 10); var c3 = askComparer.Compare(o1, o3); //this test does not always pass. if timespan between c1 and c2 is too small. Assert.AreEqual(-1, c3, string.Format("AskComparer: Timestamp comparison failed. {0} vs {1}", o1.UTCTimestamp.Ticks, o3.UTCTimestamp.Ticks)); var c4 = askComparer.Compare(o3, o1); Assert.AreEqual(1, c4, "BidComparer: Timestamp comparison not symmetric"); //try to get two orders with the same timestamp. //depending on environment, it could take a variable amout of time. Try 10 times and then give up var o4 = new ExchangeOrder(4, 1, OrderSide.Sell, OrderType.Limit, 10, 10); var o5 = new ExchangeOrder(5, 1, OrderSide.Sell, OrderType.Limit, 10, 9); int counter = 0; while (o4.UTCTimestamp != o5.UTCTimestamp && counter < 10) { o4 = new ExchangeOrder(4, 1, OrderSide.Sell, OrderType.Limit, 10, 10); o5 = new ExchangeOrder(5, 1, OrderSide.Sell, OrderType.Limit, 10, 9); counter++; } if (counter == 10) { Assert.Inconclusive("failed to create two orders with the same timestamp. Could not test RemainingVolume comparison"); } else { var c5 = askComparer.Compare(o4, o5); Assert.AreEqual(-1, c5, string.Format("AskComparer: If price and timestamp are equal, the order with the most remaining volume should win. {0} vs {1}", o4.RemainingVolume, o5.RemainingVolume)); } }
public void TestSortedSetOfOrders() { //ATTENTION sorted set is in descending order. [best order, ..., worst order] var bids = ImmutableSortedSet.Create(comparer: OrderComparer.DescBidComparer()); var b1 = new ExchangeOrder(1, 1, OrderSide.Buy, OrderType.Limit, 10, 10); bids = bids.Add(b1); var b2 = new ExchangeOrder(2, 1, OrderSide.Buy, OrderType.Limit, 9, 3); bids = bids.Add(b2); Assert.AreEqual(1, bids[0].ID); Assert.AreEqual(2, bids[1].ID); var b3 = new ExchangeOrder(3, 1, OrderSide.Buy, OrderType.Limit, 11, 5); bids = bids.Add(b3); Assert.AreEqual(3, bids[0].ID); Assert.AreEqual(1, bids[1].ID); Assert.AreEqual(2, bids[2].ID); var b4 = new ExchangeOrder(4, 1, OrderSide.Buy, OrderType.Limit, 10, 8); bids = bids.Add(b4); Assert.AreEqual(3, bids[0].ID); Assert.AreEqual(1, bids[1].ID); Assert.AreEqual(4, bids[2].ID); Assert.AreEqual(2, bids[3].ID); var b5 = new ExchangeOrder(5, 1, OrderSide.Buy, OrderType.Limit, 9.5m, 4); bids = bids.Add(b5); Assert.AreEqual(3, bids[0].ID); Assert.AreEqual(1, bids[1].ID); Assert.AreEqual(4, bids[2].ID); Assert.AreEqual(5, bids[3].ID); Assert.AreEqual(2, bids[4].ID); var asks = ImmutableSortedSet.Create(comparer: OrderComparer.DescAskComparer()); var a1 = new ExchangeOrder(1, 1, OrderSide.Sell, OrderType.Limit, 10, 10); asks = asks.Add(a1); var a2 = new ExchangeOrder(2, 1, OrderSide.Sell, OrderType.Limit, 11, 4); asks = asks.Add(a2); Assert.AreEqual(1, asks[0].ID); Assert.AreEqual(2, asks[1].ID); var a3 = new ExchangeOrder(3, 1, OrderSide.Sell, OrderType.Limit, 9, 5); asks = asks.Add(a3); Assert.AreEqual(3, asks[0].ID); Assert.AreEqual(1, asks[1].ID); Assert.AreEqual(2, asks[2].ID); var a4 = new ExchangeOrder(4, 1, OrderSide.Sell, OrderType.Limit, 10, 8); asks = asks.Add(a4); Assert.AreEqual(3, asks[0].ID); Assert.AreEqual(1, asks[1].ID); Assert.AreEqual(4, asks[2].ID); Assert.AreEqual(2, asks[3].ID); var a5 = new ExchangeOrder(5, 1, OrderSide.Sell, OrderType.Limit, 10.5m, 2); asks = asks.Add(a5); Assert.AreEqual(3, asks[0].ID); Assert.AreEqual(1, asks[1].ID); Assert.AreEqual(4, asks[2].ID); Assert.AreEqual(5, asks[3].ID); Assert.AreEqual(2, asks[4].ID); }