ios - Sorting an NSDictionary, descending. How do I send options with the `compare:options:` selector? -
i attempting sort nsdictionary.
from apple docs see can use keyssortedbyvalueusingselector
:
nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithint:63], @"mathematics", [nsnumber numberwithint:72], @"english", [nsnumber numberwithint:55], @"history", [nsnumber numberwithint:49], @"geography", nil]; nsarray *sortedkeysarray = [dict keyssortedbyvalueusingselector:@selector(compare:)];
which gives:
// sortedkeysarray contains: geography, history, mathematics, english
but want:
// sortedkeysarray contains: english, mathematics, history, geography
i have read can use compare:options:
, , nsstringcompareoptions
change comparison compare in other direction.
however don't understand how send compare:options:
with option in selector.
i want this:
nsarray *sortedkeysarray = [dict keyssortedbyvalueusingselector:@selector(compare:options:nsorderdescending)];
how should switch order of comparison?
related: https://discussions.apple.com/thread/3411089?start=0&tstart=0
option 1: use comparator invoke -compare:
in reverse: (thanks dan shelly!)
nsarray *blocksortedkeys = [dict keyssortedbyvalueusingcomparator: ^(id obj1, id obj2) { // switching order of operands reverses sort direction return [objc2 compare:obj1]; }];
just reverse descending , ascending return statements , should want.
option 2: reverse array have:
Comments
Post a Comment