Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 296
AmitSri Avatar asked Jan 23 '23 04:01

AmitSri


2 Answers

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

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

like image 114
tig Avatar answered Jan 24 '23 17:01

tig


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
like image 43
AmitSri Avatar answered Jan 24 '23 18:01

AmitSri