AmitSri
AmitSri

Reputation: 1225

How to sort filenames correctly like Finder in objective-c

I am binding NStableView with NSMutableArray contiaining filenames and other file details. Simple biniding and sorting with compare: is not properly sorting file names like finder. Please let me know if i need to define custom selector for sorting filenames and how?

Upvotes: 4

Views: 1295

Answers (2)

tig
tig

Reputation: 27800

Starting from Mac OS X 10.6 just use -[NSString localizedStandardCompare:].

For earlier systems see Technical Q&A QA1159: Sorting Like the Finder

Upvotes: 8

AmitSri
AmitSri

Reputation: 1225

using custom selector in TableColumn Attribute special thanks to KennyTM

// category on NSString for custom comparison
@interface NSString (FilesComparison)
- (NSComparisonResult)compareFiles:(NSString*)file;
@end
@implementation NSString (FilesComparison) 
- (NSComparisonResult)compareFiles:(NSString*)file {
    return [(NSString *)self compare:file options: NSCaseInsensitiveSearch|NSNumericSearch];
}
@end

Upvotes: 2

Related Questions