Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data NSSortDescriptor with two keys

Is it possible to sort a request by more than one Core Data attribute key?

sort1 = NSSortDescriptor(key: "list_item", ascending: true, selector: #selector(NSString.caseInsensitiveCompare))
sort2 = NSSortDescriptor(key: "list_item_completed", ascending: true)

request.sortDescriptors = [sort1]
//request.sortDescriptors = [sort1, sort2]

I've tried creating an array and adding it to the sortDescriptor but it only reflected the first sort. I have even tried also using the sortDescriptors in series with no luck:

request.sortDescriptors = [sort1]
request.sortDescriptors = [sort2]
like image 360
Stingalingaling Avatar asked Oct 26 '25 09:10

Stingalingaling


1 Answers

The second sort descriptor gets applied only if the first sort descriptor returns equal for the objects that are get compared.

I assume you want to show the list of items completed first in alphabetical order and them un completed again in alphabetical order. In that case, Jus add sort2 to the array first and then add sort1.

like image 165
Arvind Avatar answered Oct 28 '25 23:10

Arvind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!