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.

  • Some tests...

    1) Adding elements to a dictionary with ascending numeric IDs:
    {0=>{dateCreated=>2022-07-22T02:06:56.544Z}, 1=>{dateCreated=>2022-07-05T21:19:29.543Z}, 2=>{dateCreated=>2022-07-03T19:56:16.458Z}, 3=>{dateCreated=>2022-07-03T19:23:36.052Z}, 4=>{dateCreated=>2022-07-02T19:18:33.175Z}, 5=>{dateCreated=>2022-07-02T19:25:24.857Z}, 6=>{dateCreated=>2022-07-06T20:43:38.871Z}, 7=>{dateCreated=>2022-07-04T13:04:02.137Z}, 8=>{dateCreated=>2022-07-02T11:55:05.368Z}, 9=>{dateCreated=>2022-07-12T15:50:46.934Z}, 10=>{dateCreated=>2022-07-12T20:11:07.487Z}, 11=>{dateCreated=>2022-07-03T12:21:00.408Z}, 12=>{dateCreated=>2022-07-02T19:28:44.656Z}, 13=>{dateCreated=>2022-07-02T12:01:56.268Z}, 14=>{dateCreated=>2022-07-02T19:13:22.379Z}, 15=>{dateCreated=>2022-07-03T19:34:30.491Z}, 16=>{dateCreated=>2022-07-04T14:31:09.288Z}, 17=>{dateCreated=>2022-07-03T19:25:50.027Z}, 18=>{dateCreated=>2022-07-12T20:05:08.805Z}, 19=>{dateCreated=>2022-07-03T19:34:32.095Z}, 20=>{dateCreated=>2022-07-12T15:50:42.961Z}, 21=>{dateCreated=>2022-07-03T19:56:14.825Z}}

    Looks as if the elements are added at the end without reordering.

    2) Adding elements with descending IDs (counting back from 59 to 0):

    The first elements seem to be added in ID order - new elements added at beginning) :

    {56=>{dateCreated=>2022-07-03T19:23:36.052Z}, 57=>{dateCreated=>2022-07-03T19:56:16.458Z}, 58=>{dateCreated=>2022-07-05T21:19:29.543Z}, 59=>{dateCreated=>2022-07-22T02:06:56.544Z}}

    But after some elements the order is not kept:

    {57=>{dateCreated=>2022-07-03T19:56:16.458Z}, 58=>{dateCreated=>2022-07-05T21:19:29.543Z}, 59=>{dateCreated=>2022-07-22T02:06:56.544Z}, 46=>{dateCreated=>2022-07-02T12:01:56.268Z}, 47=>{dateCreated=>2022-07-02T19:28:44.656Z}, 48=>{dateCreated=>2022-07-03T12:21:00.408Z}, 49=>{dateCreated=>2022-07-12T20:11:07.487Z}, 50=>{dateCreated=>2022-07-12T15:50:46.934Z}, 51=>{dateCreated=>2022-07-02T11:55:05.368Z}, 52=>{dateCreated=>2022-07-04T13:04:02.137Z}, 53=>{dateCreated=>2022-07-06T20:43:38.871Z}, 54=>{dateCreated=>2022-07-02T19:25:24.857Z}, 55=>{dateCreated=>2022-07-02T19:18:33.175Z}, 56=>{dateCreated=>2022-07-03T19:23:36.052Z}}

    Converting of time string into epoch to use it as key will not solve the issue.

    I continue with other implementations and let the notifications out for now. Perhaps I can get in contact with the API developer to add another endpoint to get a sorted array instead of dictionary.

  • Have you tried implementing a more efficient sort like quick sort or merge sort?

    With such a small amount of data it may not make much difference though.

  • No, I haven't. It's a quick and dirty sort (loop) just to check the string compare. For 50 entries it won't make a big difference. I can try to exchange the string compare with a string->number conversion to avoid the compare loops. But for sorting the array I have to slice and recreate the array on every insert. That's perhaps as bad for perfomance as the sort algorithm itself.

  • can you show me the hashCode() function?

  • I only used the nuneric index from the loop over the keys array.

  • is it any problem to show this function?

    have you checked is it this function called by dictionary?

  • I haven't used hash function yet. Does it mean I have to implement a dedicated hash function for this dictionary to manipulate the has value I don't know exactly what you mean. Do you have an example?

  • not only hash function but class and it hasCode() functions that allow to know how dictionary is sorted 

    have you read all post?

    have you read beginning of

    developer.garmin.com/.../Dictionary.html

  • Ah, now it's abit more clear. I missed the context of your proposal of using a class and hash function.

    So I use a new instance of my "idClass" instead of a integer or string as ID for the inserted dictionary element?
    The class has a constructor to insert the datetime representation which must be converted to a sortable int value. And this value i return in the hashCode function. And the hope is, that this "manipulated" hash will automatically sort the dictionary. So I only have to bringe the datetime to a int32 value.

    Am I right with this method?

  • yes and this isClass should be the key