コード例 #1
0
ファイル: UnitTests.cs プロジェクト: yaronthurm/Lib
        public void TestSorting()
        {
            // Sort ascending
            List <ClassForComparing> list = new List <ClassForComparing> {
                new ClassForComparing {
                    ID = 1, StringValue = "abc", IntValue = 1
                },
                new ClassForComparing {
                    ID = 2, StringValue = "abc", IntValue = 2
                },
                new ClassForComparing {
                    ID = 3, StringValue = "xyz", IntValue = 1
                },
                new ClassForComparing {
                    ID = 4, StringValue = "xyz", IntValue = 2
                }
            };

            // all ascending
            list.Sort(new GenericComparer <ClassForComparing>(x => x.StringValue, x => x.IntValue));
            this.CheckListOrder(list, 1, 2, 3, 4);

            // one aspect descending, the other ascending
            list.Sort(new GenericComparer <ClassForComparing>(x => OrderedComparable.Desc(x.StringValue), x => x.IntValue));
            this.CheckListOrder(list, 3, 4, 1, 2);

            // All descending
            list.Sort(new GenericComparer <ClassForComparing>(x => OrderedComparable.Desc(x.StringValue), x => OrderedComparable.Desc(x.IntValue)));
            this.CheckListOrder(list, 4, 3, 2, 1);
        }
コード例 #2
0
ファイル: UnitTests.cs プロジェクト: yaronthurm/Lib
        public void TestCompare()
        {
            // Sort ascending
            ClassForComparing a = new ClassForComparing {
                ID = 1, StringValue = "abc", IntValue = 1
            };
            ClassForComparing b = new ClassForComparing {
                ID = 2, StringValue = "abc", IntValue = 2
            };
            ClassForComparing c = new ClassForComparing {
                ID = 3, StringValue = "xyz", IntValue = 1
            };

            // Comparing 'a' to 'b' by string ascending, Should be a == b
            int ret = a.CompareToByAspects(b, x => x.StringValue);

            Assert.AreEqual(ret, 0);

            // Comparing 'a' to 'c' by string ascending, Should be a < c
            ret = a.CompareToByAspects(c, x => x.StringValue);
            Assert.AreEqual(ret, -1);

            // Comparing 'a' to 'c' by string descending, Should be a > c
            ret = a.CompareToByAspects(c, x => OrderedComparable.Desc(x.StringValue));
            Assert.AreEqual(ret, 1);

            // Comparing 'a' to 'b' by string ascending and then by int ascending, Should be a < b
            ret = a.CompareToByAspects(b, x => x.StringValue, x => x.IntValue);
            Assert.AreEqual(ret, -1);

            // Comparing 'a' to 'b' by string ascending and then by int ascending, Should be a < b (still)
            ret = a.CompareToByAspects(b, x => x.StringValue, x => OrderedComparable.Asc(x.IntValue));
            Assert.AreEqual(ret, -1);

            // Comparing 'a' to 'b' by string ascending and then by int descending, Should be a > b
            ret = a.CompareToByAspects(b, x => x.StringValue, x => OrderedComparable.Desc(x.IntValue));
            Assert.AreEqual(ret, 1);
        }