System.Globalization.CompareInfo.Compare C# (CSharp) Method

Compare() public method

public Compare ( String string1, int offset1, int length1, String string2, int offset2, int length2, CompareOptions options ) : int
string1 String
offset1 int
length1 int
string2 String
offset2 int
length2 int
options CompareOptions
return int
        public unsafe virtual int Compare(String string1, int offset1, int length1, String string2, int offset2, int length2, CompareOptions options)
        {
            if (options == CompareOptions.OrdinalIgnoreCase) 
            {
                int result = String.Compare(string1, offset1, string2, offset2, length1<length2 ? length1 : length2, StringComparison.OrdinalIgnoreCase);
                if ((length1 != length2) && result == 0) 
                    return (length1 > length2? 1: -1);    
                return (result);
            }
            
            // Verifiy inputs
            if (length1 < 0 || length2 < 0)
            {
                throw new ArgumentOutOfRangeException((length1 < 0) ? "length1" : "length2", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
            }
            if (offset1 < 0 || offset2 < 0)
            {
                throw new ArgumentOutOfRangeException((offset1 < 0) ? "offset1" : "offset2", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
            }
            if (offset1 > (string1 == null ? 0 : string1.Length) - length1)
            {
                throw new ArgumentOutOfRangeException("string1", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength"));
            }    
            if (offset2 > (string2 == null ? 0 : string2.Length) - length2)
            {
                throw new ArgumentOutOfRangeException("string2", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength"));
            }
            if ((options & CompareOptions.Ordinal) != 0) 
            {
                if (options == CompareOptions.Ordinal) 
                {
                    //
                    // Check for the null case.
                    //
                    if (string1 == null) 
                    {
                        if (string2 == null) 
                        {
                            return (0);
                        }
                        return (-1);
                    }
                    if (string2 == null) 
                    {
                        return (1);
                    }
                
                    int result = String.nativeCompareOrdinalEx(string1, offset1, string2, offset2, (length1 < length2 ? length1 : length2));
                    if ((length1 != length2) && result == 0) 
                    {
                        return (length1 > length2? 1: -1);    
                    }
                    return (result);                
                } else 
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_CompareOptionOrdinal"), "options"); 
                }
            }
            if ((options & ValidCompareMaskOffFlags) != 0) 
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options"); 
            }

            //
            // Check for the null case.
            //
            if (string1 == null) 
            {
                if (string2 == null) 
                {
                    return (0);
                }
                return (-1);
            }
            if (string2 == null) 
            {
                return (1);
            }


            // Call native code to do comparison
            return (CompareRegion(m_pSortingTable, this.m_sortingLCID, string1, offset1, length1, string2, offset2, length2, options));
        }

Same methods

CompareInfo::Compare ( String string1, String string2 ) : int
CompareInfo::Compare ( String string1, String string2, CompareOptions options ) : int
CompareInfo::Compare ( String string1, int offset1, String string2, int offset2 ) : int
CompareInfo::Compare ( String string1, int offset1, String string2, int offset2, CompareOptions options ) : int
CompareInfo::Compare ( String string1, int offset1, int length1, String string2, int offset2, int length2 ) : int
CompareInfo::Compare ( void pSortingTable, int sortingLCID, String string1, String string2, CompareOptions options ) : int

Usage Example

Esempio n. 1
0
		internal int AddSorted(TreeNodeEx node)
		{
			int pos = 0;
			string text = node.Text;
			if (childCount > 0)
			{
				System.Globalization.CompareInfo compare = System.Windows.Forms.Application.CurrentCulture.CompareInfo;
				// Simple optimization if added in sort order
				if (compare.Compare(children[(childCount - 1)].Text, text) <= 0)
					pos = childCount;
				else
				{
					// Binary Search
					int i = 0;
					int j = childCount;
					while (i < j)
					{
						int mid = (i + j) / 2;
						if (compare.Compare(children[mid].Text, text) <= 0)
							i = mid + 1;
						else
							i = mid;
					}
					pos = i;
				}
			}

			node.SortChildren();
			InsertNodeAt(pos, node);
			if (treeView != null && node == treeView.SelectedNode)
			{
				treeView.SelectedNode = node;
			}
			return pos;
		}
All Usage Examples Of System.Globalization.CompareInfo::Compare