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.

  • What server are you trying to use?  If it's something you did, it's "your sever"

    Why not explain what you want to do and the pieces involved (what runs where and what gets data from where)

  • I'm not sending any data to any server, well I am, but not in the context of Garmin CIQ. I was arguing that intercepting data between user and a third party (the webservice that spits out JSON) by another third party is/was dubious.

    I was thinking about this as well. It might work fine and dandy for a personal app that no-one else uses. But if you are going to capture traffic from/to a watch owner, you are probably also capturing the tokens between them and the source data provider. Which might not be something you would want. I would like to know these things as a user.

    I'm really not sure where my app and my server are coming from in this discussion.

  • If I'm understanding your scenario correctly, the CIQ app would still need to have the token / user data in order to make the web request in the first place, whether or not the dev implements a proxy.

    So from the user's POV, they have to trust the CIQ dev no matter what.

    If the user was aware that a 3rd party cloud service was used as a proxy, then they would also have to trust that service. (The dev has to trust that service regardless).

    On the balance of things, users probably implicitly trust Amazon, Google, Microsoft  and Apple more than they trust John Q Random CIQ Dev. Whether it's good or bad, we trust some or all of these entities with critical information on a daily basis.

    I understand the concern, but as a user I have to trust the CIQ dev no matter what, so if the CIQ dev discloses that they use a 3rd party cloud service as a proxy, that probably doesn't change my trust level (unless they say they are using some unknown or untrusted cloud service, like [Random Hostile Nation's] Discount Cloud Platform).

    I mean if I think the dev is malicious and chooses to log and/or exfiltrate my user data, if they disclose that they also use a proxy for network traffic probably won't change my mind either way.

    Like say I use an app which requires me to supply my own token for some weather API. I don't know if it makes a difference to me whether they access that weather API directly or whether they use a proxy. Either way, the dev has the ability to steal my token if they want to, or perhaps to accidentally abuse the TOS and get my account banned.

    I agree that it would be nice for devs to disclose this, but it could also cause unwarranted concern from non-tech savvy users. Idk ¯\_(ツ)_/¯

  • I figured out how to quote \o/

    Just don't quote emojis No entry sign

  • maybe solution is simpler

    1, make new class X which has all necessary data from source dictionary item

    2. write function in this class hashCode() that convert time from time stamp to number (e.g. epoch time)

    3. for each item in source dictionary make objectn O of X

    4. put each O into new  dictionary D, probably D will be sorted correctly

  • 4. put each O into new  dictionary D, probably D will be sorted correctly

    Like I said, I checked the hashcodes of the UUIDs in OPs data, and they didn't seem to appear in increasing order in the OPs dictionary. (Which was strange to me.) Maybe I made a mistake in my test.

    That would be a pretty good solution if it works tho.

  • You have to write  hashCode() that return good sorted value.

    System shouldn't scan all dictionary to find key so probably use any index and this index is hashCode(). i have not tested it, it is only idea.

  • You have to write  hashCode() that return good sorted value.

    I get it. My point was, OP showed us UUIDs in his dictionary:

    ronnywinkler said:

    And the result of makeWebRequest:

    {603d7912-948d-4a82-a746-11c1bee40755=>{dateCreated=>2022-07-22T02:06:56.544Z, iconObj=>null, id=>603d7912-948d-4a82-a746-11c1bee40755, ownerUri=>homey:manager:updates, readBy=>[], icon=>null, excerpt=>Es ist ein Software-Update verfügbar (**8.0.3**)., meta=>{}, priority=>INFORMATIVE, roles=>[owner, user]}, ad0e4205-3a38-4aaa-8272-2edb64bd2585=>{dateCreated=>2022-07-05T21:19:29.543Z, iconObj=>null, id=>ad0e4205-3a38-4aaa-8272-2edb64bd2585, ownerUri=>homey:manager:energy, readBy=>[], icon=>null, excerpt=>Batterie von **Dach Süd** ist jetzt OK., meta=>{}, priority=>INFORMATIVE, roles=>[owner, user]}, 38101bf3-eba8-4148-bf7b-f8b17f311af7=>{dateCreated=>2022-07-03T19:56:16.458Z, iconObj=>null, id=>38101bf3-eba8-4148-bf7b-f8b17f311af7, ownerUri=>homey:manager:flow, readBy=>[], icon=>null, excerpt=>SQL-Error: Invalid type for token 'id' expected 'stri

    So the order of the keys in his dictionary is:

    603d7912-948d-4a82-a746-11c1bee40755
    ad0e4205-3a38-4aaa-8272-2edb64bd2585
    38101bf3-eba8-4148-bf7b-f8b17f311af7

    I wrote test code which takes those 3 strings and calls hashCode() on them.

    System.println("0x" + "603d7912-948d-4a82-a746-11c1bee40755".hashCode().format("%X"));
    System.println("0x" + "ad0e4205-3a38-4aaa-8272-2edb64bd2585".hashCode().format("%X"));
    System.println("0x" + "38101bf3-eba8-4148-bf7b-f8b17f311af7".hashCode().format("%X"));
    This is the result:
    0x67D587C1
    0xDF88771E
    0x39F52058

    As you can see the hash codes don't appear to be in order. Like I said maybe I did something wrong here.

    Here's the same test without formatting the hashcodes:

    System.println("603d7912-948d-4a82-a746-11c1bee40755".hashCode());
    System.println("ad0e4205-3a38-4aaa-8272-2edb64bd2585".hashCode());
    System.println("38101bf3-eba8-4148-bf7b-f8b17f311af7".hashCode());

    Result:


    1742047169
    -544704738
    972365912
  • no...

    class X

    {

       function hashCode()

       {

           return (covert time_string to epoch);

  • You don't understand.

    I'm saying that my tests show that in the existing data, the keys aren't sorted by hashcode.

    OP's dictionary has keys in this order:
    603d7912-948d-4a82-a746-11c1bee40755
    ad0e4205-3a38-4aaa-8272-2edb64bd2585
    38101bf3-eba8-4148-bf7b-f8b17f311af7

    But the corresponding hashcodes are: 1742047169, -544704738 and 972365912 (not in sorted order by hashcode value)

    But if I'm wrong, and the keys are sorted by hashcode, then your idea is pretty good.

    (Sure, if you do the test for the simple case of "a", "b", and "c", then the hashcodes are sorted (100, 101, and 102))