0

rsort not working

Hi all -

rsort is leaving out the first item in my array, see pic.

Screen Shot 2021-08-21 at 19.50.27

Forumla2 is the the regular sort order and Formula is the reverse sort. As you can see 7 stays in the first position.

 

Here is my code:

 

let curRec := this;
let x := ((select TeamResults)[Location = curRec.Location] order by SumPnts);
let conX := concat(x.SumPnts);
let xArray := split(conX, ",");
let rsArray := rsort(xArray);
rsArray

 

Thanks for any help.

7 replies

null
    • Sean
    • 3 yrs ago
    • Reported - view

    Hi Fred,

     

    I think the rsort formula can be simplified to this...

     

    let curRec := this;
    rsort((select TeamResults)[Location = curRec.Location] order by SumPnts)

    • Fred
    • 3 yrs ago
    • Reported - view

    Sadly it doesn't. This change brings up an empty formula.

    rsort only works on arrays, thus the many steps to turn the results into an array. I can probably shorten some steps, but i find it helpful to break the steps out.

    • Sean
    • 3 yrs ago
    • Reported - view

    The result of a select statement is an array. You can use a select statement inside the count() function to see how many items are in the array. I didn't use a location field in my test database...

     

    Screen Shot 2021-08-22 at 10.59.49 AM

    • Fred
    • 3 yrs ago
    • Reported - view

    I guess my coding was off. I can shorten the code and leave out the concat and split.

     

    When I do this:

     

    let curRec := this;
    rsort(select TeamResults)[Location = curRec.Location].SumPnts

     

    I get this:

     

    Screen Shot 2021-08-22 at 10.06.26

    If I do this:

     

    let curRec := this;
    let x := (select TeamResults)[Location = curRec.Location];
    rsort(x.SumPnts)

     

    then I get the desired results:

    Screen Shot 2021-08-22 at 10.06.36

     

    So thanks for the help.

    • Fred
    • 3 yrs ago
    • Reported - view

    I just realized my mistake in the formula 7. I forgot the ( ) for the rsort. Once I add that in, all is well. Thanks.

    • John_Halls
    • 3 yrs ago
    • Reported - view

    Hi Fred

     

    I wanted to understand why your initial code was not working and I have found out why. By concatenating and then splitting you introduced a leading space into each element except the first one. So rather than having

    7

    17

    18

    ...

    55

     

    you had

     

    7

     17

     18

    ...

     55

     

    rsort this and 7 has the largest value and will remain at the top.

     

    Regards John

    • Sean
    • 3 yrs ago
    • Reported - view

    Hi John,

     

    I completely overlooked that. Even though spaces were added, that's not why it's being sorted that way. When the values were concatenated, they were converted to text and then were sorted by ASCII value instead of numeric value.

     

    Regards

Content aside

  • 3 yrs agoLast active
  • 7Replies
  • 402Views