Cadenza : Cadenza Namespace

NaturalStringComparer Class

Provides IComparer and IComparer<string> implementations to sort strings in a "natural" fashion.

public sealed class NaturalStringComparer : IComparer<string>, IComparer

See Also

Cadenza.Collections.IEnumerableCoda.OrderByNatural``1(IEnumerable<``0>, Func<``0, string>)
Cadenza.Collections.IEnumerableCoda.SortNatural(IEnumerable<string>)

Thread Safety

This type is thread safe.

Remarks

"Natural" string comparison compares strings as humans would compare them, not as computers do. Thus the string "10" sorts after "2", whereas "normally" "10" would sort before "2" because normal string comparison works a character at a time, and "1" is before "2".

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

Members

See Also: Inherited members from object.

Public Properties

[read-only]
static
Default NaturalStringComparer . Returns the default Cadenza.NaturalStringComparer instance to use for comparing strings.

Public Methods

Compare (string, string) : int
Returns the sort order of two string instances.

Explicitly Implemented Interface Members

IComparer.Compare Returns the sort order of two object instances.

Extension Methods

static
Just<T> (this T) : Maybe<T>
Create a new Cadenza.Maybe<T> instance initialized to a specified value. The returned value will not be Maybe<T>.Nothing.
static
Match<TSource,TResult> (this TSource, params Func<TSource, Maybe<TResult>>[]) : TResult
Converts the TSource instance self into a TResult.
static
ToMaybe<T> (this T) : Maybe<T>
Create a new Cadenza.Maybe<T> instance initialized to a specified value. The returned value may be Maybe<T>.Nothing.
static
TraverseBreadthFirst<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<TResult>
Traverse a tree in a breadth-first fashion, converting each encountered node.
static
TraverseBreadthFirstWithParent<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<KeyValuePair<TSource, TResult>>
Traverse a tree in a breadth-first fashion, converting each encountered node.
static
TraverseDepthFirst<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<TResult>
Traverse a tree in a depth-first fashion, converting each encountered node.
static
TraverseDepthFirstWithParent<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<KeyValuePair<TSource, TResult>>
Traverse a tree in a depth-first fashion, converting each encountered node.
static
With<TSource,TResult> (this TSource, Func<TSource, TResult>) : TResult
Supports chaining otherwise temporary values.

Member Details

Compare Method

Returns the sort order of two string instances.

public int Compare (string x, string y)

Parameters

x
The first string to compare.
y
The second string to compare.

Returns

The return value is a negative number, zero, or a positive number reflecting the sort order of a as compared to b. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

Value Condition
A negative number a < b.
Zero a == b.
A positive number a > b.
Note: A null reference is considered to compare less than any other non-null object, and equal to any other null reference, independent of the underlying Type of either object.

Remarks

This method compares x to y using a "natural" string comparison algorithm, so that strings with embedded numeric data sort by the numeric data in conjunction with the surrounding string, instead of treating the numeric data as part of the string itself.

This allows "foo10" to sort after "foo2", as is usually expected when looking at sorted text, instead of having "foo10" sort before "foo2", as Comparer<string> would do.

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

Default Property

Returns the default Cadenza.NaturalStringComparer instance to use for comparing strings.

public static NaturalStringComparer Default { get; }

Value

A Cadenza.NaturalStringComparer instance to use for comparing strings.

Remarks

This field is read-only.

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

System.Collections.IComparer.Compare Method

Returns the sort order of two object instances.

int System.Collections.IComparer.Compare (object x, object y)

Parameters

x
The first object to compare.
y
The second object to compare.

Returns

The return value is a negative number, zero, or a positive number reflecting the sort order of a as compared to b. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

Value Condition
A negative number a < b.
Zero a == b.
A positive number a > b.
Note: A null reference is considered to compare less than any other non-null object, and equal to any other null reference, independent of the underlying Type of either object.

Remarks

This method calls NaturalStringComparer.Compare(string, string), passing x.ToString() and y.ToString() as arguments.

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0