I'm using NSSortDescriptor
to sort an NSArray
of NSDictionary
items. Works great, just what I needed... except that I'd like for the dictionary items that have blank values for my sort key to show up at the end of the sorted list. Is there a way to easily accomplish this? Or will I have to create some custom sorting functions? And no, I don't want to just set it to DESC order... I'd like sorted results to look like A, A, B, B, C, blanks, blank.
相关问题
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Sort TreeView Automatically Upon Adding Nodes
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
Figured this out after seeing another example using a custom comparison. Here is the code I ended up with:
So the comparer returns NSOrderedSame if both strings are empty... calls the regular comparison function if both strings are non-empty... if only one string is empty, it reverses the normal order of that comparison. Voila!
Do the Sort using NSNumericSort instead of NSDiacriticInsensitive or NSCaseInsensitive. Since the whitespace is probably 256 in ascii code it will be sent to the back of the list.
Edit: I misread the question and thought the intent was to put strings with leading spaces first in sort order (which they should anyway).
You want something like this, I think. This is completely untested and doesn't take multiple leading spaces into account. I'm sure there's a more elegant way, but it should get the job done.