コード例 #1
0
        public void AlphaNumericSorter_SortsNumbersWithChars()
        {
            var comp = new AlphaNumericComparator();

            Assert.IsTrue(comp.Compare("2", "10") < 0);
            Assert.IsTrue(comp.Compare("2a", "2") > 0);
            Assert.IsTrue(comp.Compare("3", "2a") > 0);
            Assert.IsTrue(comp.Compare("003", "3") == 0);
            Assert.IsTrue(comp.Compare("0000", "00000000000000000001") < 0);
        }
コード例 #2
0
        public void AlphaNumericSorter_SortsNumbersWithChars()
        {
            var comp = new AlphaNumericComparator();

            Assert.IsTrue(comp.Compare("2", "10") < 0);
            Assert.IsTrue(comp.Compare("2a", "2") > 0);
            Assert.IsTrue(comp.Compare("3", "2a") > 0);
            Assert.IsTrue(comp.Compare("003", "3") == 0);
            Assert.IsTrue(comp.Compare("0000", "00000000000000000001") < 0);
        }
コード例 #3
0
        public static bool WithCondition(this string @this,
            string keyword,
            SearchCondition condition)
        {
            var comparator = new AlphaNumericComparator();
            switch (condition)
            {
                case SearchCondition.Contains:
                    return @this.Contains(keyword);
                case SearchCondition.NotContains:
                    return [email protected](keyword);
                case SearchCondition.MoreThan:
                    return comparator.Compare(@this, keyword) == 1;
                case SearchCondition.LessThan:
                    return comparator.Compare(@this, keyword) == -1;
                case SearchCondition.EqualTo:
                    return comparator.Compare(@this, keyword) == 0;
                case SearchCondition.NotEqualTo:
                    return comparator.Compare(@this, keyword) != 0;
                case SearchCondition.LongerThan:
                    if (int.TryParse(keyword, out var longer))
                        return @this.Length > longer;

                    throw new ArgumentException($@"Unable to parse {nameof(keyword)} to number.", nameof(keyword));
                case SearchCondition.ShorterOrEqual:
                    if (int.TryParse(keyword, out var shorter))
                        return @this.Length <= shorter;

                    throw new ArgumentException($@"Unable to parse {nameof(keyword)} to number.", nameof(keyword));
                case SearchCondition.HasValue:
                    return @this.IsNotNullOrEmpty();
                case SearchCondition.IsEmpty:
                    return @this.IsNullOrEmpty();
                case SearchCondition.StartsWith:
                    return @this.StartsWith(keyword);
                case SearchCondition.DoesNotStartWith:
                    return [email protected](keyword);
                case SearchCondition.EndsWith:
                    return @this.EndsWith(keyword);
                case SearchCondition.DoesNotEndWith:
                    return [email protected](keyword);
                case SearchCondition.Undefined:
                default:
                    throw new ArgumentOutOfRangeException(nameof(condition), condition, null);
            }
        }