2012-04-21 9 views
2

NSStringをNSMutable配列にトークン化しました。 NSSortDescriptorを使用してその配列を文字列の長さでソートできます。そして、私は特定の言葉で最長から始まる方法を実行できます。NSMutableArrayの元のソート順を元に戻す方法は?

これは私の問題が浮上する場所です。配列の元のソート順を復元して、配列を編集した文字列に戻す必要があります。

答えて

2

私の答えは基本的にスティーブと似ていますが、元の配列を保持し、ソートされたコピーを作成し、ソートされたコピーを処理してからそれを破棄します。したがって、たとえば

NSArray *yourOriginalArray = ...whatever...; 

// do processing 
for(id object in [yourOriginalArray sortedArrayUsing<whatever means>]) 
{ 
    // if you're doing processing that doesn't change the identity of 
    // the object then there's no need to worry about this, but supposing 
    // you want to adjust the identity (eg, the source array is full of 
    // immutable objects and you want to replace them) then... 

    NSUInteger originalIndex = [yourOriginalArray indexOfObject:object]; 

    id replacementObject = [self mutatedFormOf:object]; 
    [yourOriginalArray replaceObjectAtIndex:originalIndex 
        withObject:replacementObject]; 
    // of course, this is only if yourOriginalArray is mutable 
} 
+0

ありがとう、私はこれが私のハードルを乗り越えるために必要なものだと思います。 – noahread

2

NSMutableArrayは、それが現在の状態であるかどうかについてはわかりません。おそらくより洗練されたソリューションがありますが、元のソート順を格納する別の配列を作成することができます。

関連する問題