views:

249

answers:

1

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.

+2  A: 
  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.
KennyTM
1) thanks, that was a typo when I transposed the code to here... 2) can I copy the dictionary entries to an array, sort and copy back? how?
Digital Robot
@Mike: Yes you can sort in the array, but when the copy them back into the dictionary the order will be messed up again. The dictionary is always orderless. You have to stick with NSArray.
KennyTM
Thanks!!!!!!!!!!
Digital Robot
Or, better than storing the time intervals as strings (assuming you have no specific reason to do that), store them as numbers. You can *and should* convert them to strings at display time using an NSNumberFormatter.
Peter Hosey