• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Smart sort
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Smart sort


  • Subject: Re: Smart sort
  • From: James Bucanek <email@hidden>
  • Date: Mon, 27 Nov 2006 08:54:36 -0700

Martin wrote on Monday, November 27, 2006:

>Hi,
>
>I'm listing a folder's content using the following method :
>[[NSFileManager defaultManager] enumeratorAtPath:@"/path/to/a/folder"].
>
>But I'd like to files names to be smart-sorted, just like the Finder
>does :
>File 1.txt
>File 2.txt
>File 10.txt
>
>Instead of what the previous method gives me :
>File 1.txt
>File 10.txt
>File 2.txt
>
>Is there a simple way to do this ?

I use UCCompareTextNoLocale using kUCCollateTypeHFSExtended.

I do a lot of work with the Carbon file APIs and wrote this catagory for NSString. The - (NSComparisonResult)hfsExtendedCompare:(NSString*)right lets me sort Cocoa collections of NSStrings.

You're free to copy this and you as you please.


---- HFSUnicodeString.h ---------------------------------

//
//  HFSUnicodeString.h
//  FacetWorkshop
//
//  Created by James Bucanek on 12/15/04.
//  Copyright 2004 Dawn to Dusk Software. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface NSString (HFSUnicodeString)

+ (NSString*)stringWithHFSUnicodeStr255:(const HFSUniStr255*)hfsStr255;

- (NSComparisonResult)hfsExtendedCompare:(NSString*)right;
- (void)getHFSUnicodeStr255:(HFSUniStr255*)hfsStr255;

@end


---- HFSUnicodeString.m ---------------------------------

//
//  HFSUnicodeString.m
//  FacetWorkshop
//
//  Created by James Bucanek on 12/15/04.
//  Copyright 2004 Dawn to Dusk Software. All rights reserved.
//

#import "HFSUnicodeString.h"


@implementation NSString (HFSUnicodeString)

+ (NSString*)stringWithHFSUnicodeStr255:(const HFSUniStr255*)hfsStr255
{
    return ([NSString stringWithCharacters:hfsStr255->unicode length:hfsStr255->length]);
}

- (NSComparisonResult)hfsExtendedCompare:(NSString*)right
{
    unsigned int leftLength = [self length];
    unsigned int rightLegnth = [right length];
    NSAssert1(leftLength<=255,@"HFS Unicode string too long (%d)",leftLength);
    NSAssert1(rightLegnth<=255,@"HFS Unicode string too long (%d)",rightLegnth);

    UniChar leftChars[256];
    UniChar rightChars[256];
    [self getCharacters:leftChars];
    [right getCharacters:rightChars];

    SInt32 order;
    OSStatus status = UCCompareTextNoLocale(kUCCollateTypeHFSExtended<<kUCCollateTypeShiftBits,leftChars,leftLength,rightChars,rightLegnth,NULL,&order);
    NSAssert1(status==0,@"UCCompareTextNoLocale error: %d",(int)status);

    if (order<0)
        return (NSOrderedAscending);
    if (order==0)
        return (NSOrderedSame);
    return (NSOrderedDescending);
}

- (void)getHFSUnicodeStr255:(HFSUniStr255*)hfsStr255
{
    hfsStr255->length = [self length];
    NSAssert1(hfsStr255->length<=255,@"HFS Unicode string too long (%d)",hfsStr255->length);
    [self getCharacters:hfsStr255->unicode];
}


@end
--
James Bucanek
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Smart sort (From: Martin <email@hidden>)

  • Prev by Date: Re: NSTableView: use sorting
  • Next by Date: Re: Smart sort
  • Previous by thread: Smart sort
  • Next by thread: Re: Smart sort
  • Index(es):
    • Date
    • Thread