Compare() публичный Метод

public Compare ( object a, object b ) : int
a object
b object
Результат int
		public void TestCompare_Culture ()
		{
			CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;

			try {
				Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");

				// the default ctor is initialized using Thread.CurrentCulture
				CaseInsensitiveComparer cic = new CaseInsensitiveComparer ();
				Assert.AreEqual (-1, cic.Compare ("I", "i"), "#A1");
				Assert.AreEqual (0, cic.Compare ("A", "a"), "#A2");

				// changing the current culture does not affect an already
				// initialized CaseInsensitiveComparer
				Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
				Assert.AreEqual (-1, cic.Compare ("I", "i"), "#B1");
				Assert.AreEqual (0, cic.Compare ("A", "a"), "#B2");

				// but it does affect new instances
				cic = new CaseInsensitiveComparer ();
				Assert.AreEqual (0, cic.Compare ("I", "i"), "#C1");
				Assert.AreEqual (0, cic.Compare ("A", "a"), "#C2");

				// if the culture is explicitly set, then the thread culture is
				// ignored
				cic = new CaseInsensitiveComparer (new CultureInfo ("tr-TR"));
				Assert.AreEqual (-1, cic.Compare ("I", "i"), "#D1");
				Assert.AreEqual (0, cic.Compare ("A", "a"), "#D2");
			} finally {
				// restore original culture
				Thread.CurrentThread.CurrentCulture = originalCulture;
			}
		}
		public void TestCompare () {
			CaseInsensitiveComparer cic = new CaseInsensitiveComparer ();

			AssertEquals(cic.Compare ("WILD WEST", "Wild West"),0);
			AssertEquals(cic.Compare ("WILD WEST", "wild west"),0);
			Assert(cic.Compare ("Zeus", "Mars") > 0);
			Assert(cic.Compare ("Earth", "Venus") < 0);
		}
		public void TestCompare ()
		{
			CaseInsensitiveComparer cic = new CaseInsensitiveComparer ();

			Assert.AreEqual (0, cic.Compare ("WILD WEST", "Wild West"));
			Assert.AreEqual (0, cic.Compare ("WILD WEST", "wild west"));
			Assert.AreEqual (1, cic.Compare ("Zeus", "Mars"));
			Assert.AreEqual (-1, cic.Compare ("Earth", "Venus"));
		}
Пример #4
0
        private int CompareProcess(object a, object b)
        {
            CaseInsensitiveComparer c = new CaseInsensitiveComparer();

            if (Ascending)
            {
                return c.Compare(a, b);
            }

            return c.Compare(b, a);
        }
        public int Compare(object x, object y)
        {
            CultureInfo ci = new CultureInfo("sv");
            CaseInsensitiveComparer aComparer = null;
			aComparer = new CaseInsensitiveComparer(ci);
            return aComparer.Compare(x, y);
        }
Пример #6
0
 public virtual int Compare( object x, object y )
 {
     ListItem a = (ListItem)x;
     ListItem b = (ListItem)y;
     CaseInsensitiveComparer c = new CaseInsensitiveComparer();
     return c.Compare(a.Text, b.Text);
 }
        public int Compare(object x, object y)
        {
            int ret;
            ExplorerListItem first = x as ExplorerListItem;
            ExplorerListItem second = y as ExplorerListItem;

            if (ColumnToSort == 3)
            {
                ret = DateTime.Compare(DateTime.Parse(first.SubItems[ColumnToSort].Text), DateTime.Parse(second.SubItems[ColumnToSort].Text));
            }
            else
            {
                CaseInsensitiveComparer comp = new CaseInsensitiveComparer();

                ret = comp.Compare(first.SubItems[ColumnToSort].Text, second.SubItems[ColumnToSort].Text);
            }

            if (SortingOrder == SortOrder.Ascending)
            {
                return ret;
            }
            else if(SortingOrder == SortOrder.Descending)
            {
                return (-ret);
            }
            else
            {
                return 0;
            }
        }
Пример #8
0
 public int Compare(object x, object y) // Assuming x, y are Strings (from ListView.Text)
 {
     if (regex.Match((string)x).Success&& regex.Match((string)y).Success)
     {
         return(Convert.ToInt32(x).CompareTo(Convert.ToInt32(y)));
     }
     else
     {
         return(ciComparer.Compare(x, y));
     }
 }
Пример #9
0
        /// <summary>
        /// 重写IComparer接口.
        /// </summary>
        /// <param name="x">要比较的第一个对象</param>
        /// <param name="y">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {
            int compareResult;

            System.Windows.Forms.ListViewItem listviewX, listviewY;

            // 将比较对象转换为ListViewItem对象
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;

            string xText = listviewX.SubItems[ColumnToSort].Text;
            string yText = listviewY.SubItems[ColumnToSort].Text;

            int    xInt, yInt;
            double xDouble, yDouble;

            // 比较,如果值为IP地址,则根据IP地址的规则排序。
            if (IsIP(xText) && IsIP(yText))
            {
                compareResult = CompareIp(xText, yText);
            }
            else if (double.TryParse(xText, out xDouble) && double.TryParse(yText, out yDouble)) //是否为小数
            {
                compareResult = CompareDouble(xDouble, yDouble);
            }
            else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //是否全为数字
            {
                //比较数字
                compareResult = CompareInt(xInt, yInt);
            }
            else
            {
                //比较对象
                compareResult = ObjectCompare.Compare(xText, yText);
            }

            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                // 因为是正序排序,所以直接返回结果
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                // 如果是反序排序,所以要取负值再返回
                return(-compareResult);
            }
            else
            {
                // 如果相等返回0
                return(0);
            }
        }
Пример #10
0
        public int Compare(object a, object b)
        {
            ListViewItem aItem = (ListViewItem)a;
            ListViewItem bItem = (ListViewItem)b;

            switch(columnToSort)
            {
                case 0:
                    return ComparePID(aItem.SubItems[0].Text, bItem.SubItems[0].Text);
                case 1:
                    return CompareProcess(aItem.SubItems[1].Text, bItem.SubItems[1].Text);
                default:
                    CaseInsensitiveComparer c = new CaseInsensitiveComparer();
                    return c.Compare(a, b);
            }
        }
Пример #11
0
        /// <summary>

        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(object x, object y)
        {
            int compareResult;

            System.Windows.Forms.ListViewItem listviewX, listviewY;

            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;
            string xText = listviewX.SubItems[ColumnToSort].Text;
            string yText = listviewY.SubItems[ColumnToSort].Text;
            int    xInt, yInt;

            if (IsIP(xText) && IsIP(yText))
            {
                compareResult = CompareIp(xText, yText);
            }
            else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt))
            {
                compareResult = CompareInt(xInt, yInt);
            }
            else
            {
                compareResult = ObjectCompare.Compare(xText, yText);
            }

            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                return(-compareResult);
            }
            else
            {
                return(0);
            }
        }
Пример #12
0
 public int Compare(object x, object y)
 {
     var lix = (ListItem) x;
     var liy = (ListItem) y;
     var c = new CaseInsensitiveComparer();
     return c.Compare(lix.Text, liy.Text);
 }
Пример #13
0
 private int Compare(VersionEntity info1, VersionEntity info2)
 {
     int result;
     CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
     result = ObjectCompare.Compare(info1.BuildTime, info2.BuildTime);
     return result;
 }
Пример #14
0
        public override bool Evaluate(RuntimeValues runtimeEnv)
        {
            object leftVal = left.GetRuntimeValue(runtimeEnv);
            object rightVal = right.GetRuntimeValue(runtimeEnv);

            IComparer comparer = new CaseInsensitiveComparer(CultureInfo.InvariantCulture);

            int compare;
            if (leftVal is IComparable)
                compare = comparer.Compare(leftVal, rightVal);
            else if (rightVal is IComparable)
                compare = -comparer.Compare(rightVal, leftVal);
            else
            {
                // Only do this if neither version is comparable.
                // The reason is IComparable objects can change the
                // meaning of Equals in surprising ways if necessary,
                // such as for version matching.
                if (opToken == XppTokenType.EQ)
                    return Equals(leftVal, rightVal);
                if (opToken == XppTokenType.NEQ)
                    return !Equals(leftVal, rightVal);

                throw new ArgumentException("Can't perform a compare on two objects that don't implement IComparable");
            }

            switch (opToken)
            {
                case XppTokenType.LT:
                    return compare < 0;
                case XppTokenType.LTE:
                    return compare <= 0;
                case XppTokenType.EQ:
                    return compare == 0;
                case XppTokenType.NEQ:
                    return compare != 0;
                case XppTokenType.GT:
                    return compare > 0;
                case XppTokenType.GTE:
                    return compare >= 0;
                default:
                    throw new ArgumentException("Program error: Unexpected comparison operator in XppCompareExpr");
            }
        }
Пример #15
0
        /// <summary>
        /// ÖØдIComparer½Ó¿Ú.
        /// </summary>
        /// <param name="x">Òª±È½ÏµÄµÚÒ»¸ö¶ÔÏó</param>
        /// <param name="y">Òª±È½ÏµÄµÚ¶þ¸ö¶ÔÏó</param>
        /// <returns>±È½ÏµÄ½á¹û.Èç¹ûÏàµÈ·µ»Ø0£¬Èç¹ûx´óÓÚy·µ»Ø1£¬Èç¹ûxСÓÚy·µ»Ø-1</returns>
        public int Compare(object x, object y)
        {
            int compareResult;

            System.Windows.Forms.ListViewItem listviewX, listviewY;

            // ½«±È½Ï¶ÔÏóת»»ÎªListViewItem¶ÔÏó
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;

            string xText = listviewX.SubItems[ColumnToSort].Text;
            string yText = listviewY.SubItems[ColumnToSort].Text;

            int      xInt, yInt;
            double   xDouble, yDouble;
            DateTime xDate, yDate;

            // ±È½Ï,Èç¹ûֵΪIPµØÖ·£¬Ôò¸ù¾ÝIPµØÖ·µÄ¹æÔòÅÅÐò¡£
            if (IsIP(xText) && IsIP(yText))
            {
                compareResult = CompareIp(xText, yText);
            }
            else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //ÊÇ·ñȫΪÊý×Ö
            {
                //±È½ÏÊý×Ö
                compareResult = CompareInt(xInt, yInt);
            }
            else if (double.TryParse(xText, out xDouble) && double.TryParse(yText, out yDouble)) //ÕûÊÇ·ñÈ«ÊǸ¡µãÐÍ
            {
                //±È½Ï¸¡µãÐÍÊý×Ö
                compareResult = CompareDouble(xDouble, yDouble);
            }
            else if (int.TryParse(xText, out xInt) && double.TryParse(yText, out yDouble)) //ÕûÐͺ͸¡µãÐÍ
            {
                //±È½Ï¸¡µãÐÍÊý×Ö
                compareResult = CompareIntAndDouble(xInt, yDouble);
            }
            else if (DateTime.TryParse(xText, out xDate) && DateTime.TryParse(yText, out yDate)) //ÕûÐͺ͸¡µãÐÍ
            {
                //±È½ÏÈÕÆÚ
                compareResult = CompareDate(xDate, yDate);
            }
            else
            {
                //±È½Ï¶ÔÏó
                compareResult = ObjectCompare.Compare(xText, yText);
            }
            // ¸ù¾ÝÉÏÃæµÄ±È½Ï½á¹û·µ»ØÕýÈ·µÄ±È½Ï½á¹û
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                // ÒòΪÊÇÕýÐòÅÅÐò£¬ËùÒÔÖ±½Ó·µ»Ø½á¹û
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                // Èç¹ûÊÇ·´ÐòÅÅÐò£¬ËùÒÔҪȡ¸ºÖµÔÙ·µ»Ø
                return(-compareResult);
            }
            else
            {
                // Èç¹ûÏàµÈ·µ»Ø0
                return(0);
            }
        }
Пример #16
0
        /// <summary>
        /// Arranges a variable's contents in alphabetical, numerical, or random order optionally removing duplicates.
        /// </summary>
        /// <param name="input">The variable whose contents to use as the input.</param>
        /// <param name="options">See the remarks.</param>
        /// <remarks>
        /// <list type="table">
        /// <listheader>
        /// <term>Name</term>
        /// <description>Description</description>
        /// </listheader>
        /// <item>
        /// <term>C</term>
        /// <description>Case sensitive.</description>
        /// </item>
        /// <item>
        /// <term>CL</term>
        /// <description>Case sensitive based on current user's locale.</description>
        /// </item>
        /// <item>
        /// <term>D<c>x</c></term>
        /// <description>Specifies <c>x</c> as the delimiter character which is <c>`n</c> by default.</description>
        /// </item>
        /// <item>
        /// <term>F <c>name</c></term>
        /// <description>Use the return value of the specified function for comparing two items.</description>
        /// </item>
        /// <item>
        /// <term>N</term>
        /// <description>Numeric sorting.</description>
        /// </item>
        /// <item>
        /// <term>P<c>n</c></term>
        /// <description>Sorts items based on character position <c>n</c>.</description>
        /// </item>
        /// <item>
        /// <term>R</term>
        /// <description>Sort in reverse order.</description>
        /// </item>
        /// <item>
        /// <term>Random</term>
        /// <description>Sort in random order.</description>
        /// </item>
        /// <item>
        /// <term>U</term>
        /// <description>Remove any duplicate items.</description>
        /// </item>
        /// <item>
        /// <term>Z</term>
        /// <description>Considers a trailing delimiter as a boundary which otherwise would be ignored.</description>
        /// </item>
        /// <item>
        /// <term>\</term>
        /// <description>File path sorting.</description>
        /// </item>
        /// </list>
        /// </remarks>
        public static void Sort(ref string input, params string[] options)
        {
            var opts = KeyValues(string.Join(",", options), true, new[] { 'f' });
            MethodInfo function = null;

            if (opts.ContainsKey('f'))
            {
                function = FindLocalRoutine(opts['f']);
                if (function == null)
                    return;
            }

            char split = '\n';

            if (opts.ContainsKey('d'))
            {
                string s = opts['d'];
                if (s.Length == 1)
                    split = s[0];
                opts.Remove('d');
            }

            string[] list = input.Split(new[] { split }, StringSplitOptions.RemoveEmptyEntries);

            if (split == '\n')
            {
                for (int i = 0; i < list.Length; i++)
                {
                    int x = list[i].Length - 1;
                    if (list[i][x] == '\r')
                        list[i] = list[i].Substring(0, x);
                }
            }

            if (opts.ContainsKey('z') && input[input.Length - 1] == split)
            {
                Array.Resize(ref list, list.Length + 1);
                list[list.Length - 1] = string.Empty;
                opts.Remove('z');
            }

            bool withcase = false;
            if (opts.ContainsKey('c'))
            {
                string mode = opts['c'];
                if (mode == "l" || mode == "L")
                    withcase = false;
                else
                    withcase = true;
                opts.Remove('c');
            }

            bool numeric = false;
            if (opts.ContainsKey('n'))
            {
                numeric = true;
                opts.Remove('n');
            }

            int sortAt = 1;
            if (opts.ContainsKey('p'))
            {
                if (!int.TryParse(opts['p'], out sortAt))
                    sortAt = 1;
                opts.Remove('p');
            }

            bool reverse = false, random = false;
            if (opts.ContainsKey(Keyword_Random[0]))
            {
                if (opts[Keyword_Random[0]].Equals(Keyword_Random.Substring(1), StringComparison.OrdinalIgnoreCase)) // Random
                    random = true;
                else
                    reverse = true;
                opts.Remove(Keyword_Random[0]);
            }

            bool unique = false;
            if (opts.ContainsKey('u'))
            {
                unique = true;
                opts.Remove('u');
            }

            bool slash = false;
            if (opts.ContainsKey('\\'))
            {
                slash = true;
                opts.Remove('\\');
            }

            var comp = new CaseInsensitiveComparer();
            var rand = new Random();

            Array.Sort(list, delegate(string x, string y)
            {
                if (function != null)
                {
                    object value = null;

                    try
                    {
                        value = function.Invoke(null, new object[] { new object[] { x, y } });
                    }
                    catch (Exception)
                    {
                    }

                    int result;

                    if (value is string && int.TryParse((string) value, out result))
                        return result;

                    return 0;
                }
                else if (x == y)
                    return 0;
                else if (random)
                    return rand.Next(-1, 2);
                else if (numeric)
                {
                    int a, b;
                    return int.TryParse(x, out a) && int.TryParse(y, out b) ?
                        a.CompareTo(b) : x.CompareTo(y);
                }
                else
                {
                    if (slash)
                    {
                        int z = x.LastIndexOf('\\');
                        if (z != -1)
                            x = x.Substring(z + 1);
                        z = y.LastIndexOf('\\');
                        if (z != -1)
                            y = y.Substring(z + 1);
                        if (x == y)
                            return 0;
                    }
                    return withcase ? x.CompareTo(y) : comp.Compare(x, y);
                }
            });

            if (unique)
            {
                int error = 0;
                var ulist = new List<string>(list.Length);
                foreach (var item in list)
                    if (!ulist.Contains(item))
                        ulist.Add(item);
                    else
                        error++;
                ErrorLevel = error;
                list = ulist.ToArray();
            }

            if (reverse)
                Array.Reverse(list);

            input = string.Join(split.ToString(), list);
        }
Пример #17
0
            int IComparer.Compare(Object x, Object y)
            {
                int[] xArray = (int[])x;
                int[] yArray = (int[])y;

                var myComp = new CaseInsensitiveComparer();

                // X first -> Ascending, y first -> Descending
                var toReturn = myComp.Compare(xArray.Sum(), yArray.Sum());

                return toReturn;
            }
Пример #18
0
 public static bool SimpleScalarValuesCompare(object _value1, object _value2, System.Type _unboxType, SimpleScalarValueCompareMode _compareMode)
 {
     bool result = false;
     CaseInsensitiveComparer comparer = new CaseInsensitiveComparer();
     //
     switch (_compareMode)
     {
         case SimpleScalarValueCompareMode.Equal:
             if (comparer.Compare(System.Convert.ChangeType(_value1, _unboxType),
                                                 System.Convert.ChangeType(_value2, _unboxType)) == 0)
                 result = true;
             break;
         case SimpleScalarValueCompareMode.LessThan:
             if (comparer.Compare(System.Convert.ChangeType(_value1, _unboxType),
                                                 System.Convert.ChangeType(_value2, _unboxType)) < 0)
                     result = true;
             break;
         case SimpleScalarValueCompareMode.GreaterThan:
             if (comparer.Compare(System.Convert.ChangeType(_value1, _unboxType),
                                                 System.Convert.ChangeType(_value2, _unboxType)) > 0)
                 result = true;
             break;
         case SimpleScalarValueCompareMode.LessThenOrEqual:
             if (comparer.Compare(System.Convert.ChangeType(_value1, _unboxType),
                                                 System.Convert.ChangeType(_value2, _unboxType)) < 0)
                 result = true;
             break;
         case SimpleScalarValueCompareMode.GreaterThenOrEqual:
             if (comparer.Compare(System.Convert.ChangeType(_value1, _unboxType),
                                                 System.Convert.ChangeType(_value2, _unboxType)) >= 0)
                 result = true;
             break;
         default:
             break;
     }
     //
     return result;
 }
Пример #19
0
 public int Compare(object x, object y)
 {
     var a = (ListItem) x;
     var b = (ListItem) y;
     var c = new CaseInsensitiveComparer();
     return c.Compare(a.Text, b.Text);
 }
Пример #20
0
        /// <summary>
        /// 重写IComparer接口.
        /// </summary>
        /// <param name="x">要比较的第一个对象</param>
        /// <param name="y">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {
            if (ColumnToSort < 0)
            {
                return(0);
            }

            int compareResult;

            System.Windows.Forms.ListViewItem listviewX, listviewY;

            // 将比较对象转换为ListViewItem对象
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;

            string xText = listviewX.SubItems[ColumnToSort].Text;
            string yText = listviewY.SubItems[ColumnToSort].Text;

            int xInt, yInt;

            // 如果是按文件名排序,去掉后缀名后比较
            if (ColumnToSort == 0)
            {
                xText = Path.GetFileNameWithoutExtension(xText);
                yText = Path.GetFileNameWithoutExtension(yText);
            }
            // 比较,如果值为IP地址,则根据IP地址的规则排序。
            if (IsIP(xText) && IsIP(yText))
            {
                compareResult = CompareIp(xText, yText);
            }
            else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //是否全为数字
            {
                try
                {
                    xInt = int.Parse(xText);
                    yInt = int.Parse(yText);
                    //比较数字
                    compareResult = CompareInt(xInt, yInt);
                }
                catch
                {
                    //比较对象
                    compareResult = ObjectCompare.Compare(xText, yText);
                }
            }
            else
            {
                //比较对象
                compareResult = ObjectCompare.Compare(xText, yText);
            }
            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                // 因为是正序排序,所以直接返回结果
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                // 如果是反序排序,所以要取负值再返回
                return(-compareResult);
            }
            else
            {
                // 如果相等返回0
                return(0);
            }
        }