2

「;」で区切られた要素を持つtxtファイル(文字列)があります。これを MSMutable 配列に読み込みます。日付に続くフィールドでソートする必要があります。整数データになります。このフィールドを文字列から取得して並べ替えるにはどうすればよいですか。何日も検索しましたが、これへの参照が見つかりません。

2012/09/17;5;-54.74 2012/09/17;76;6.53 2012/09/17;66;6.53 2012/09/17;69;6.53 2012/09/17;60;6.53 2012/09/ 17;96;6.53 2012/09/17;86;6.53 2012/09/17;77;6.53

ありがとう、

ロン

4

1 に答える 1

1

sortUsingComparator:次のようにメソッドを使用できます。

[array sortUsingComparator: ^(id lhs, id rhs) {
    // Get the string between the first and the second semicolons:
    NSString *obj1 = [[lhs componentsSeparatedByString:@";"] objectAtIndex:1];
    NSString *obj2 = [[rhs componentsSeparatedByString:@";"] objectAtIndex:1];
    // Compare the two strings as integers:
    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
于 2012-09-19T21:43:46.860 に答える