如何按 NSString 数组中包含的数字对数组进行排序? (按升序排列)

发布于 2024-11-29 14:38:55 字数 2950 浏览 1 评论 0原文

我想知道是否可以根据其中包含的数值对 NSStrings 的 NSArray 进行排序。基本上它是进程 ID 和进程名称的列表,我想根据进程 ID 对进程进行排序。该数组如下所示:

"286             Google Chrome He",
"1209             ibtool",
"0             kernel_task",
"1             launchd",
"10             kextd",
"11             notifyd",
"12             diskarbitrationd",
"13             configd",
"14             syslogd",
"15             DirectoryService",
"16             distnoted",
"18             ntpd",
"22             SystemStarter",
"25             securityd",
"28             mds",
"29             mDNSResponder",
"30             loginwindow",
"31             KernelEventAgent",
"33             hidd",
"34             fseventsd",
"36             dynamic_pager",
"42             blued",
"43             autofsd",
"46             WDSmartWareD",
"47             WDDMService",
"61             coreservicesd",
"62             WindowServer",
"71             HWPortCfg",
"72             HWNetCfg",
"159             socketfilterfw",
"165             cvmsServ",
"177             coreaudiod",
"191             vmnet-bridge",
"196             vmnet-dhcpd",
"198             vmnet-netifup",
"200             vmnet-dhcpd",
"204             vmnet-natd",
"206             vmnet-netifup",
"220             launchd",
"224             Dock",
"225             SystemUIServer",
"226             Finder",
"228             pboard",
"229             fontd",
"240             UserEventAgent",
"247             AirPort Base Sta",
"252             iprint-listener",
"253             StatusMenu",
"254             Dropbox",
"271             dbfseventsd",
"275             Google Chrome",
"295             Google Chrome He",
"298             AppleSpell",
"634             Google Chrome He",
"696             Google Chrome He",
"730             Microsoft Word",
"733             Microsoft Databa",
"736             Microsoft AU Dae",
"1095             usbmuxd",
"1110             Xcode",
"1171             Interface Builde",
"1282             Interface Builde",
"1283             Interface Builde",
"1475             Google Chrome He",
"1531             Google Chrome He",
"1533             Google Chrome He",
"1681             Google Chrome He",
"1682             Google Chrome He",
"1686             Google Chrome He",
"1687             Google Chrome He",
"1692             Google Chrome He",
"1945             Google Chrome He",
"2088             Keynote",
"2268             Google Chrome He",
"2326             Google Chrome He",
"2481             Google Chrome He",
"2545             Google Chrome He",
"2596             Google Chrome He",
"2766             mdworker",
"2933             Google Chrome He",
"2963             iPhone Simulator",
"2967             SimulatorBridge",
"2970             lsd",
"2971             SpringBoard",
"2982             ocspd",
"2998             installd",
"3000             TableViewControl",
"3001             taskgated",
"3002             gdb-i386-apple-d"

非常感谢任何帮助!

I'm wondering if it is possible to sort an NSArray of NSStrings based on a number value contained within. Basically its a list of process ID's and process names, and I would like to sort the processes based on their process ID. The array looks like this :

"286             Google Chrome He",
"1209             ibtool",
"0             kernel_task",
"1             launchd",
"10             kextd",
"11             notifyd",
"12             diskarbitrationd",
"13             configd",
"14             syslogd",
"15             DirectoryService",
"16             distnoted",
"18             ntpd",
"22             SystemStarter",
"25             securityd",
"28             mds",
"29             mDNSResponder",
"30             loginwindow",
"31             KernelEventAgent",
"33             hidd",
"34             fseventsd",
"36             dynamic_pager",
"42             blued",
"43             autofsd",
"46             WDSmartWareD",
"47             WDDMService",
"61             coreservicesd",
"62             WindowServer",
"71             HWPortCfg",
"72             HWNetCfg",
"159             socketfilterfw",
"165             cvmsServ",
"177             coreaudiod",
"191             vmnet-bridge",
"196             vmnet-dhcpd",
"198             vmnet-netifup",
"200             vmnet-dhcpd",
"204             vmnet-natd",
"206             vmnet-netifup",
"220             launchd",
"224             Dock",
"225             SystemUIServer",
"226             Finder",
"228             pboard",
"229             fontd",
"240             UserEventAgent",
"247             AirPort Base Sta",
"252             iprint-listener",
"253             StatusMenu",
"254             Dropbox",
"271             dbfseventsd",
"275             Google Chrome",
"295             Google Chrome He",
"298             AppleSpell",
"634             Google Chrome He",
"696             Google Chrome He",
"730             Microsoft Word",
"733             Microsoft Databa",
"736             Microsoft AU Dae",
"1095             usbmuxd",
"1110             Xcode",
"1171             Interface Builde",
"1282             Interface Builde",
"1283             Interface Builde",
"1475             Google Chrome He",
"1531             Google Chrome He",
"1533             Google Chrome He",
"1681             Google Chrome He",
"1682             Google Chrome He",
"1686             Google Chrome He",
"1687             Google Chrome He",
"1692             Google Chrome He",
"1945             Google Chrome He",
"2088             Keynote",
"2268             Google Chrome He",
"2326             Google Chrome He",
"2481             Google Chrome He",
"2545             Google Chrome He",
"2596             Google Chrome He",
"2766             mdworker",
"2933             Google Chrome He",
"2963             iPhone Simulator",
"2967             SimulatorBridge",
"2970             lsd",
"2971             SpringBoard",
"2982             ocspd",
"2998             installd",
"3000             TableViewControl",
"3001             taskgated",
"3002             gdb-i386-apple-d"

Any help is very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-12-06 14:38:55

对于您的情况,最简单的方法是使用排序描述符:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"integerValue" ascending:YES];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

For your case, the simplest way to do it is with a sort descriptor:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"integerValue" ascending:YES];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
乱世争霸 2024-12-06 14:38:55

是的,您可以使用以下 NSArray 实例方法:

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

您需要定义一个带有三个参数的 int 函数:要比较的两个数组元素和一个上下文。它返回一个 NSComparisonResult(-1、0 或 1)。例如:

NSInteger firstWordIntCompare(id stringLeft, id stringRight, void *context)
{
    // Extract number from stringLeft and stringRight
    NSArray *wordsLeft = [stringLeft componentsSeparatedByString:@" "];
    NSArray *wordsRight = [stringRight componentsSeparatedByString:@" "];
    NSString *firstWordLeft = wordsLeft.count ? [wordsLeft objectAtIndex:0] : nil;
    NSString *firstWordRight = wordsRight.count ? [wordsRight objectAtIndex:0] : nil;
    int intLeft = [firstWordLeft intValue];
    int intRight = [firstWordRight intValue];

    if (intLeft < intRight)
        return NSOrderedAscending;
    else if (intLeft > intRight)
        return NSOrderedDescending;
    else 
        return NSOrderedSame;
}

然后通过使用您的函数调用 NSArray 方法来返回排序后的数组:

NSArray *sortedArray = [origArray sortedArrayUsingFunction:firstWordIntCompare context:NULL];

Yes, you can use the following NSArray instance method:

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

You'll need to define an int function that takes three arguments: two array elements to compare, and a context. It returns a NSComparisonResult (-1, 0 or 1). For example:

NSInteger firstWordIntCompare(id stringLeft, id stringRight, void *context)
{
    // Extract number from stringLeft and stringRight
    NSArray *wordsLeft = [stringLeft componentsSeparatedByString:@" "];
    NSArray *wordsRight = [stringRight componentsSeparatedByString:@" "];
    NSString *firstWordLeft = wordsLeft.count ? [wordsLeft objectAtIndex:0] : nil;
    NSString *firstWordRight = wordsRight.count ? [wordsRight objectAtIndex:0] : nil;
    int intLeft = [firstWordLeft intValue];
    int intRight = [firstWordRight intValue];

    if (intLeft < intRight)
        return NSOrderedAscending;
    else if (intLeft > intRight)
        return NSOrderedDescending;
    else 
        return NSOrderedSame;
}

Then return the sorted array by calling the NSArray method with your function:

NSArray *sortedArray = [origArray sortedArrayUsingFunction:firstWordIntCompare context:NULL];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文