singrdk/base/Applications/Runtime/Full/System/Collections/CaseInsensitiveComparer.cs

53 lines
1.8 KiB
C#
Raw Permalink Normal View History

2008-03-05 09:52:00 -05:00
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
2008-11-17 18:29:00 -05:00
//============================================================
//
// Class: CaseInsensitiveComparer
//
//============================================================
namespace System.Collections
{
2008-03-05 09:52:00 -05:00
using System;
using System.Collections;
using System.Globalization;
//| <include path='docs/doc[@for="CaseInsensitiveComparer"]/*' />
public class CaseInsensitiveComparer : IComparer {
public static readonly CaseInsensitiveComparer Default = new CaseInsensitiveComparer();
//| <include path='docs/doc[@for="CaseInsensitiveComparer.CaseInsensitiveComparer"]/*' />
public CaseInsensitiveComparer() {
}
//| <include path='docs/doc[@for="CaseInsensitiveHashCodeProvider.DefaultInvariant"]/*' />
public static CaseInsensitiveComparer DefaultInvariant
{
get
{
return Default;
}
}
// Behaves exactly like Comparer.Default.Compare except that the comparison is case insensitive
// Compares two Objects by calling CompareTo. If a ==
// b,0 is returned. If a implements
// IComparable, a.CompareTo(b) is returned. If a
// doesn't implement IComparable and b does,
// -(b.CompareTo(a)) is returned, otherwise an
// exception is thrown.
//
//| <include path='docs/doc[@for="CaseInsensitiveComparer.Compare"]/*' />
public int Compare(Object a, Object b) {
String sa = a as String;
String sb = b as String;
if (sa != null && sb != null)
return CompareInfo.Compare(sa, sb, CompareOptions.IgnoreCase);
else
return Comparer.Default.Compare(a,b);
}
}
}