William  Sterling
William Sterling

Reputation: 177

find a method sort NSArray on obj-C what equivalent python sort (timsort)

The python server api will check the request signature, and the mechanism is get all values, and sort, then append a secret and md5 hash. But I don't know how to simulate the sort() function in obj-C. There's the python make_sign code:

def make_sign(api_secret, params=[]):
"""
    >>> make_sign("mysecret",[1,'2','string'])
    'AACF0E5427EECA3490C5642C9916785'
"""
_params = [p for p in params if p is not None]
_params.sort()
_params.insert(0, api_secret)
strs = ''.join(_params)
mds = md5(strs.encode('utf-8')).hexdigest()
return mds.upper()

Upvotes: 0

Views: 42

Answers (1)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16660

You can sort an instance of NSArray with the -sortedArray… methods. This starts from simple sorting on a key (for the object itself use self) up to sort functions and closures. Just pick a method!

However, sorting strings is not that unambiguously it might be. I. e. in German there are different ways to sort "A" and "Ä" or "s", "ss" and "ß". So to have an identical sort order, you maybe have to use -sortedArrayUsingComparator: and write your own comparison.

Upvotes: 1

Related Questions