Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:57:37+00:00 2026-05-30T14:57:37+00:00

I want to sort an NSTableView column with IPv4 addresses using an SortDescriptor. This

  • 0

I want to sort an NSTableView column with IPv4 addresses using an SortDescriptor. This I have so far:

NSArray *devicesSortDescriptor = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"ip" ascending:YES comparator:^NSComparisonResult(id ipstring1, id ipstring2) {
        const char * ipch1 = [ipstring1 cStringUsingEncoding:NSASCIIStringEncoding];
        const char * ipch2 = [ipstring2 cStringUsingEncoding:NSASCIIStringEncoding];
        struct in_addr ip1, ip2;

        inet_aton(ipch1, &ip1);
        inet_aton(ipch2, &ip2);

        if (ip1.s_addr > ip2.s_addr)
            return NSOrderedDescending;
        else if (ip1.s_addr == ip2.s_addr)
            return NSOrderedSame;
        else
            return NSOrderedAscending;
    }]];

And this is the result:

strange sorted IPv4s

What am I doing wrong? I mean if ip1.s_addr > ip2.s_addr then its of cause NSOrderedDescending.. o_O

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:57:38+00:00Added an answer on May 30, 2026 at 2:57 pm

    I just found out, that all you need is a numeric comparator:

    Objective-C:

    comparator:^(NSString *a, NSString *b) {
            return [a compare:b options:NSNumericSearch];
        }   
    

    Swift:

    comparator: {
            (a, b) -> NSComparisonResult in
    
            let result = a.compare(b as! String, options: NSStringCompareOptions.NumericSearch)
            return result
        }
    

    🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to sort this block on date column: [domain1.com 18-Jan-2011 #^/ domain2.com 20-Aug-2011
I have a NSTableView with 3 columns. I can sort using any of the
I want to sort on a certain column only for rows containing a certain
I want to sort results set first based on one column and then based
I have List I want to sort Desc by Priority, which is int and
I have an array that I want to sort based on the value of
I want to sort array of random numbers using MPI library. The idea is
I want to sort a vector using std::sort, but my sort method is a
I want to sort objects using by one of their attributes. As of now,
I want to sort a set of data using C implementation. I wonder which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.