Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • sorting values stored in a local

    Hi All,

    Is there a way of sorting values stored in a local? For example:

    Code:
    local direction 1 0 2 4 3
    should be transformed to:

    Code:
    local direction 0 1 2 3 4
    Thanks in advance!

    Ariel

  • #2
    Code:
    local direction 1 0 2 4 3
    local sorted_direction: list sort direction
    di "`sorted_direction'"
    See

    Code:
    help macrolists

    Comment


    • #3
      Beautiful! Thank you Andrea!

      Comment


      • #4
        Note that this approach will sort the values in the local as strings, not as numbers, so the preceding won't work if there are multiple digit numbers in the local:

        Code:
        local direction 11 101 49297 14 3
        local sorted_direction: list sort direction
        di "`sorted_direction'"
        101 11 14 3 49297
        Some use of Mata can solve this more generally, although perhaps there is an easier way than what I found:
        Code:
        local direction 11 101 49297 14 3
        local sorted_direction: list sort direction
        di "Bad: `sorted_direction'"
        //
        local direction = subinstr("`direction'", " ", "\ ", .)
        mata: st_local("sorted_direction",  invtokens(strofreal(sort(`direction',1))'))
        di "Good: `sorted_direction'"





        Comment

        Working...
        X