0

I have an array of data like so.

{
"first_name"="John",
"last_name"="Lock"
};
{
"first_name"="John",
"last_name"="Lee"
};
{
"first_name"="Melinda",
"last_name"="Abman"
};

I was able to sort by first name like so:

NSArray *sortedArray = [tablearray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES]]];

But is there a simple way to sort by firstname then lastname? Basically do the equivalent of SELECT * FROM users ORDER BY first_name ASC, last_name ASC ?

1
  • Manually you can sort, rebuild your Model(array), and show it in View(NSTableView). Same thing I did. Also, Thanks for posting this question if any better solution we can get :) Commented Nov 15, 2012 at 13:30

1 Answer 1

2

sortedArrayUsingDescriptors: takes an array of NSSortDescriptor's so you can simply keep adding sortDescripters.

NSArray *sortDescriptors = @[
  [NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES],
  [NSSortDescriptor sortDescriptorWithKey:@"last_name" ascending:YES]
];

NSLog(@"%@", [array sortedArrayUsingDescriptors:sortDescriptors]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.