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 don't know how system converted json to dictionary

    writing some tests doesn't cost a lot...

  • and b a c has the same hashes because it's probably local var id

  • you don't know how system converted json to dictionary

    writing some tests doesn't cost a lot...

    I agree. Like I said it's a good idea.

    I also literally wrote tests which seem to indicate that dictionary values aren't always sorted by hashcode, but it doesn't hurt for OP to try.

    and b a c has the same hashes because it's probably local var id

    I didn't say they have the same hashes, I said their hashes are 100, 101, and 102. And they're not symbols, they're strings.

    var dict = {
    "c" => "3",
    "a" => "1"
    };
    dict.put("b", "2");
    System.println(dict);
    System.println(dict.keys());
    System.println(dict.keys()[0].hashCode());
    System.println(dict.keys()[1].hashCode());
    System.println(dict.keys()[2].hashCode());

    Result (sorted):

    {a=>1, b=>2, c=>3}
    [a, b, c]
    100
    101
    102

    I also did the following test with OPs UUIDs and got a different order than him (the exact opposite in fact). (still apparently not sorted)

    var dict2 = {};

    dict2.put("603d7912-948d-4a82-a746-11c1bee40755", "2");
    dict2.put("ad0e4205-3a38-4aaa-8272-2edb64bd2585", "2");
    dict2.put("38101bf3-eba8-4148-bf7b-f8b17f311af7", "2");
    System.println(dict2);
    System.println(dict2.keys());
    System.println(dict2.keys()[0].hashCode());
    System.println(dict2.keys()[1].hashCode());
    System.println(dict2.keys()[2].hashCode());

    Result (apparently not sorted):

    {38101bf3-eba8-4148-bf7b-f8b17f311af7=>2, ad0e4205-3a38-4aaa-8272-2edb64bd2585=>2, 603d7912-948d-4a82-a746-11c1bee40755=>2}
    [38101bf3-eba8-4148-bf7b-f8b17f311af7, ad0e4205-3a38-4aaa-8272-2edb64bd2585, 603d7912-948d-4a82-a746-11c1bee40755]
    972365912
    -544704738
    1742047169

  • so changing

    2022-07-05T21:19:29.543Z

    to

    hash 20220705211929543

    can sort dictionary good

  • so changing

    2022-07-05T21:19:29.543Z

    to

    hash 20220705211929543

    can sort dictionary good

    Yes, I understand the general concept. It only works if dictionary values are always sorted by hashcode.

    Originally you assumed that dictionary values are in insertion order and that wasn't the case. Now you are assuming that dictionary values are always sorted by hashcode...

    2022-07-05T21:19:29.543Z

    to

    hash 20220705211929543

    This literally won't work...

    - if you mean 20220705211929543 as a number, it's too big to fit in a 32-bit hashcode

    - if you mean "20220705211929543" as a string, then the hashcodes probably won't be in the correct order that you want.

    i.e. if string X > string Y, it doesn't follow that hashcode(string X) > hashcode(string Y)

    You need to convert to seconds since the epoch (or something similar)

  • but a bit bigger than int :-)

  • but a bit bigger than int :-)

    yep

  • but gregorian and value () can help

  • but gregorian and value () can help

    Yes. The concept is good but it relies on the assumption that dictionary values are *always* sorted by hashcode. It also means that the code has to parse the dates, which could add too many steps to the algorithm (and trigger the watchdog).

    If dictionary values are *not* always sorted by hashcode, then maybe OP can try quicksort on a minimal data representation (e.g. two flat arrays with date and index to original array), to try to minimize the number of steps so they don't trigger the watchdog.

  • First thanks to all fo ryour ideas and help. I couldn't participate at the discussion due to the timezone (sleep time here in Germany ;-))

    Some words about your ideas:

    - I don't want to use a proxy sever. It's a smarthome system and for this reason personal and home data is exchanged
    - I could use a proxy app on the smarthome system to preprocess the data. But that would prevent users of the cloud service to use the CIQ widget. So I try to keep at the WebAPI to use the common standard.

    So I can try to convert the date string into a numeric value vor eaysier compare. And I can try to create a dictionary using the date string/num as key to check wheather dict/hash is sorted (I can't believe).

    But if all is not working right, then it's ok, too., The data I try to convert are notifications from the smarthome hub, so only information. COntrolling devices is working and that's the main part.