Duck
Duck

Reputation: 35953

NSMutableDictionary of NSMutableSets... sorting this out

I have a NSMutableDictionary of NSMutableSets.

Each set entry is a string, something like this:

NSMutableSet *mySet = [NSMutableSet setWithObjects: 
  [NSString stringWithFormat:@"%f", time1],
  [NSString stringWithFormat:@"%f", time2],
  [NSString stringWithFormat:@"%f", time3], 
  nil];
// time 1,2,3, are  NSTimeInterval variables

then I store each set on the dictionary using this:

NSString *rightNowString = [NSString stringWithFormat:@"%f", rightNow];
[myDict setValue:mySet forKey:rightNow];
// rightNow is NSTimeInterval
// myDict is a NSMutableDictionary

as rightNow key can occur out of order, I end with a NSDictionary that is not ordered by rightNow.

How can I sort this NSDictionary by its keys considering that they are numbers stored as strings on the dictionary...?

I don't care for ordering the sets, just the dictionary.

thanks for any help.

Upvotes: 0

Views: 869

Answers (1)

kennytm
kennytm

Reputation: 523344

  1. If the values are NSTimeInterval, then use %f or %g etc instead of %d. The NSTimeInterval is a double.
  2. You cannot sort objects inside an NSDictionary or NSSet. If you need persistent order, you have to use an NSArray, or switch to Objective-C++ and use std::map.

Upvotes: 2

Related Questions