Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
destruk
Binge Watcher

Note about the Sort custom routine

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

0 Kudos
6 REPLIES 6
RokuNB
Roku Guru

Re: Note about the Sort custom routine

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
0 Kudos
destruk
Binge Watcher

Re: Note about the Sort custom routine

Good to know RokuNB - maybe if you guys didn't keep including outdated sample code like this then developers wouldn't keep using it.
https://github.com/rokudev/videoplayer- ... lUtils.brs

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 
0 Kudos
RokuNB
Roku Guru

Re: Note about the Sort custom routine

You are welcome, @destruk! 8-)

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
0 Kudos
destruk
Binge Watcher

Re: Note about the Sort custom routine

Thanks RokuNB.
0 Kudos
the_mace
Channel Surfer

Re: Note about the Sort custom routine

"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")

That does not seem to work. Thoughts?
0 Kudos
RokuNB
Roku Guru

Re: Note about the Sort custom routine

"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:
Brightscript Debugger> a=[ { metadata:{id:3}, name:"Betty"}, { metadata:{id:1}, name:"Carol"}, { metadata:{id:2}, name:"Anne"} ]

Brightscript Debugger> for each elem in a: elem["metadata.id"] = elem.metadata.id: next: ? formatJSON(a)
[{"metadata":{"id":3},"metadata.id":3,"name":"Betty"},{"metadata":{"id":1},"metadata.id":1,"name":"Carol"},{"metadata":{"id":2},"metadata.id":2,"name":"Anne"}]

Brightscript Debugger> a.sortBy("metadata.id"): ? formatJSON(a)
[{"metadata":{"id":1},"metadata.id":1,"name":"Carol"},{"metadata":{"id":2},"metadata.id":2,"name":"Anne"},{"metadata":{"id":3},"metadata.id":3,"name":"Betty"}]
0 Kudos