Dictionary access by index

I get a dictionary with a list of IDs and values like:

dict = {
  GUID1=> {...},
  GUID2=> {...},
  GUID3=> {...},
}

The list is sorted by datetime (a field inside the {...}.
I need to read only the last 10 entries. So I tried to get the key array with dict.keys( ) and loop over it. But this key list is not sorted like the dictionary. The keys are GUIDs and that's why  I can't sort by it.
And the dictionary I can only access by key, not by index - or is it possible?

Has anyone an example how to access the dictionary by index - or if it's possible to get the key array in same order like the dictionary.

Many thanks.

  • You are writing so much. I can't even work while I'm interrupted by looking forward your answers :-).

    1) Milliseconds are not needed. The order by timestamp ist ok.

    2) Using 2 array would mean:

    - reding keys from dictionary => keys[]

    - looping over keys, creating moment values and storing them in a new array monents[{moment, key}]

    - then I still have to sort the moment array (by moment, not by strings, what hopefully would be mot perfomant)

    - then I have a ordered time array, can loop (in correct order) and read the key, rec dictionary by key.

    Sounds good. Some hints for sorting the array? There is no other way than split (slice) and recombining?

  • Try quicksort. It's an in-place sorting algorithm, so you shouldn't have to split and recombine the array. (Sorting is accomplished by swapping 2 elements at time.)

    - reding keys from dictionary => keys[]

    To save space you could use the indices from Dictionary.values(), since you have to loop over all the values anyway. It might not be that important.

    - then I still have to sort the moment array (by moment, not by strings, what hopefully would be mot perfomant)

    Yes and as you sort the timestamps, you also manipulate the keys/indices array in the exact same way.

  • When you save Moment.value() it's a 32 bit number so easy to compare for a sort and cheap to save.

  • A short feedback...the current version is:

    - I read the keys from the original dictionary into a array (dict.keys( ))

    - loop over the keys, for each one:

    - fill a new array with {time=> .., id=> ... } where id is the key from the dictionary and time is Moment.value() from the time string. The Moment values (year, montha...) are read with subtring( ) from the UTC string.

    - do a bubble sort on the time array comparing the moment integers

    Now I have a sorted time array with belonging keys. With the key I can read the entry from the original dictionary. And I'm free to show all or hide entries (e.g. show only newest 10).

    That's running now in the simulator. I hope it's fast enough on the devices. I will see.
    My idea of sorted insert into the array was very inperformant I think because of the split and re-merge of the array. A simple bubble sort is much more efficient.

    So many thanks for all the help and suggestions. I hope this problem is solved, so will here from me with a new one :-)