Forum Discussion

prajwalshetty's avatar
10 years ago

roAssociative Array

Hi,

Is there any way we can avoid roAssociative Array default sorting behavior using the key

for ex {
b: "test",
a : "test1",
c : "test3"
}

it auto sorts to a,b,c.

Thanks in advance for the help.

Prajwal

2 Replies

  • I don't think so, but you're not supposed to rely on AA sorting order anyway. To really have the sort order to be reliable, I would just use a standard array.
  • RokuKC's avatar
    RokuKC
    Roku Employee
    "prajwalshetty" wrote:
    Hi,

    Is there any way we can avoid roAssociative Array default sorting behavior using the key

    for ex {
    b: "test",
    a : "test1",
    c : "test3"
    }

    it auto sorts to a,b,c.

    Thanks in advance for the help.

    Prajwal


    As Komag said, you shouldn't rely on the order of enumeration on an associative array.

    http://sdkdocs.roku.com/display/sdkdoc/ ... r=expTOexp[STEPexp]/ENDFOR
    Objects that have no intrinsic order (like AssociativeArray) are enumerated in apparent random order


    In the current implementation, the enumeration order depends on a combination of key hashes and order of item creation.
    It's unlikely to be in alphabetical sorted order for any non-trivial and non-contrived example.

    I don't think your example of b,a,c would enumerate as a,b,c even, so I wonder if there is more to your use case than the example?

    But again as Komag said, if you care about preserving arbitrary item order than using an ordered type such roArray or roList might serve better.