开发者

Implementing and sorting associative array in Objective-C

开发者 https://www.devze.com 2023-02-25 18:00 出处:网络
In many languages, like PHP, there is what\'s known as associative array, that is, an array that consists of Key=>Value pairs, that can be sorted according to keys or to values.

In many languages, like PHP, there is what's known as associative array, that is, an array that consists of Key=>Value pairs, that can be sorted according to keys or to values. my question is:

  1. is the NSDictionary object the easiest way to create such array ?
  2. if yes, is there a ready method to sort an NSDictionary object according to values it holds (of course, while开发者_开发知识库 maintaining key=>value relationship, that is, the right key stays with the right value after the sort ends), plus I found this methos keysSortedByValueUsingSelector(@compare:) but it sorts according to keys only.

thanks for help


Ok, if everyone insists (see comments) ;)

You should read again: Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values using...

So it does not sort by keys. It sorts by values and returns the keys in the corresponding order.

You can sort the keys by getting them first (allKeys) and then sort them.

0

精彩评论

暂无评论...
验证码 换一张
取 消