If your channel uses this code - usually located in generalUtils.brs - be sure to change your sort key values to either all upper case or all lower case first. Otherwise it will place “ZZ Top” in front of “Zombie” – since “ZZ” is capitalized and the routine appears to sort based on the ASCII table of characters. (Capital letters have a lower numeric value than lowercase)
'******************************************************
'Insertion Sort
'Will sort an array directly, or use a key function
'******************************************************
Sub Sort(A as Object,key=invalid As dynamic)
If Type(A)<>"roArray" Return
If key=invalid
For i=1 To A.Count()-1
value=A[i]
j=i-1
While j>=0 And A[j]>value
A[j+1]=A[j]
j=j-1
End While
A[j+1]=value
Next
Else
If Type(key)<>"Function" Return
For i=1 To A.Count()-1
valuekey=key(A[i])
value=A[i]
j=i-1
While j>=0 And key(A[j])>valuekey
A[j+1]=A[j]
j=j-1
End While
A[j+1]=value
Next
End If
End Sub
I have a better idea - just don’t use that code!
A year ago (rOS 7.1) roArray functions Sort(), SortBy(), Reverse() were added - use these, can do case-insensitive sort too. https://sdkdocs.roku.com/display/sdkdoc/ifArraySort
That sample - the last update is dated as August 2016, but Roku OS 7.2 was released June 21, 2016, so that sample should have been using the latest and greatest functions the firmware has to offer. It makes me wonder what else the firmware does that none of the code references or gives an indication about.
Jun 21, 2016 - The Roku OS 7.2 release
A look at the copyright header of that file would give you idea it’s 7 years old. Feel invited to leave your edifying feedback for the maintainer of the specific github project by clicking on the “issues” tab in that project.
The new sort functionality is by leaps and bounds better, so IMO that fn should be removed
“RokuNB” wrote:
I have a better idea - just don’t use that code!
A year ago (rOS 7.1) roArray functions Sort(), SortBy(), Reverse() were added - use these, can do case-insensitive sort too. https://sdkdocs.roku.com/display/sdkdoc/ifArraySort
What if I want to do something like this:
a=[ { metadata:{id:3}, name:“Betty”}, { metadata:{id:1}, name:“Carol”}, { metadata:{id:2}, name:“Anne”} ]
a.SortBy(“metadata.id”)
“the_mace” wrote:
What if I want to do something like this:
a=[ { metadata:{id:3}, name:“Betty”}, { metadata:{id:1}, name:“Carol”}, { metadata:{id:2}, name:“Anne”} ]
a.SortBy(“metadata.id”)
It doesn’t work that way. There is no “metadata.id” key in these AAs, just “matadata” and “name”. And also you cannot sort by “metadata” because the value of that is another AA - it can only sort by strings or numbers. Sorting the above by “name” will work however. If you want to be able to sort by a.metadata.id, you will have to pull it one level up - either by design or artificially, like i am doing here for the sake of demonstration: