0

Assumed having the class Account with Name and accountNo both as NSStrings. I want to sort an Array of Accounts either by name or by accountNo. The sort should be done localized.

If I use NSSortDescriptors as described in the Guides I have the option to pass localizedCaseInsensitiveCompare: to the Array, but the numbers are not sorted correctly (100 is sorted before 99). I have not found a way to trigger the option NSNumericSearch for the search. This means I have to use sortedArrayUsingFunctions with a function in every class along the chain. It allows me to sort with correct numerical values but this is not localized and very functional.

If you have answers to both questions (SortDescriptors with Numerically correct sort or sortFunctions with localized and numeric correct sort) please provide me both

Thanks

4

1 に答える 1

1

NSSortDescriptor並べ替えたいプロパティごとに異なる を作成できます。各ソート記述子は、異なる比較セレクターを使用できます。文字列にはlocalizedCaseInsensitiveCompare:独自のメソッドを使用でき、数値には のようなカスタム ソート セレクタを使用できますcompareNumeric:

NSString次に、これを行うメソッドを定義するカテゴリを追加するだけですcompareNumeric:

- (NSComparisonResult) compareNumeric:(id)other {
  return [self compare:other options:NSNumericSearch];
}
于 2009-11-18T17:59:57.570 に答える