Your Digital Media Has Never Looked So Good

 
User avatar
destruk
Topic Author
Posts: 2505
Joined: Sat Dec 18, 2010 4:58 pm

Note about the Sort custom routine

Mon Apr 03, 2017 12:56 pm

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

 
User avatar
RokuNB
Posts: 299
Joined: Fri Mar 31, 2017 2:22 pm

Re: Note about the Sort custom routine

Mon Apr 03, 2017 5:46 pm

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
 
User avatar
destruk
Topic Author
Posts: 2505
Joined: Sat Dec 18, 2010 4:58 pm

Re: Note about the Sort custom routine

Mon Apr 03, 2017 6:30 pm

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 
 
User avatar
RokuNB
Posts: 299
Joined: Fri Mar 31, 2017 2:22 pm

Re: Note about the Sort custom routine

Sat Apr 08, 2017 11:28 pm

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
 
User avatar
destruk
Topic Author
Posts: 2505
Joined: Sat Dec 18, 2010 4:58 pm

Re: Note about the Sort custom routine

Sun Apr 09, 2017 10:11 pm

Thanks RokuNB.
 
the_mace
Posts: 12
Joined: Wed Jun 07, 2017 8:59 am

Re: Note about the Sort custom routine

Wed Jul 12, 2017 2:11 pm

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?
 
User avatar
RokuNB
Posts: 299
Joined: Fri Mar 31, 2017 2:22 pm

Re: Note about the Sort custom routine

Thu Jul 13, 2017 7:12 pm

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"}]

Who is online

Users browsing this forum: No registered users and 2 guests