Instance Method インスタンスメソッド

sortedArrayUsingFunction:context:

Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator. ある配列を返します、それは、受け手側の配列の持つ要素を昇順で比較関数comparatorによって定義されるとおりに列記するものです。

Declaration 宣言

- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void *))comparator 
                                          context:(void *)context;

Discussion 議論

The new array contains references to the receiving array’s elements, not copies of them. それらのコピーではなく、受け手側の持つ要素に対する参照を含んでいる新しい配列。

The comparison function is used to compare two elements at a time and should return NSOrderedAscending if the first element is smaller than the second, NSOrderedDescending if the first element is larger than the second, and NSOrderedSame if the elements are equal. Each time the comparison function is called, it’s passed context as its third argument. This allows the comparison to be based on some outside parameter, such as whether character sorting is case-sensitive or case-insensitive. 比較関数は、2つの要素を一度に比較するために使われます、そして最初の要素が2番目よりも小さいならばNSOrderedAscendingを、最初の要素が2番目よりも大きいならばNSOrderedDescendingを、およびそれらの要素が同等ならばNSOrderedSameを返さなければなりません。比較関数が呼び出されるたびに、それはそれの3番目の引数としてcontextを渡されます。これは、比較がいくつかの外部パラメータに基づくことを可能にします、例えば文字列ソートが大文字小文字等(ケース)考慮であるかまたは大文字小文字等非考慮であるかなど。

Given anArray (an array of NSNumber objects) and a comparison function of this type: anArrayNSNumberオブジェクトの配列)と、この型の比較関数を与えられて:


NSInteger intSort(id num1, id num2, void *context)
{
    int v1 = [num1 intValue];
    int v2 = [num2 intValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

A sorted version of anArray is created in this way: anArrayのソートされたバージョンは、この方法で作成されます:


NSArray *sortedArray; sortedArray = [anArray sortedArrayUsingFunction:intSort context:NULL];

See Also 参照

Sorting ソートする