Forums

Find answers, ask questions, and connect with our
community all around the world.

Home Forum Omnis General Forum Dynamic sorting of lists

  • Dynamic sorting of lists

    Posted by Uwe Smidt on January 31, 2026 at 9:47 am

    Dear $all,

    I am writing a method to sort lists, where I pass the the fields to be sorted as row(‘fieldname1′,’fieldname2′,…,’fieldnameN’). How do I build the $sort parameters in order to make it work? This, and MANY other attempts did not work (however, it does some sorting, but I have not understanding the logic of it…):

    Calculate llList as $cinst

    For liCurrentCol from 1 to prSortByFields.$colcount

    Calculate lcSortField as prSortByFields.[liCurrentCol]

    Calculate lcSortOrder as con(lcSortOrder,llList.$sqlclassname,'.',lcSortField,',kFalse,')

    End For

    Calculate lcSortOrder as left(lcSortOrder,len(lcSortOrder)-1)

    Do llList.$sort(lcSortOrder)

    Thanks in advance

    Uwe

    Uwe Smidt replied 1 day, 2 hours ago 2 Members · 4 Replies
  • 4 Replies
  • Andreas Pfeiffer

    Administrator
    January 31, 2026 at 11:51 am

    Hi Uwe,

    The $sort requires a reference to the list column, not just the name of the list column. Only if using a list that is defined from other variables it might work like this.

    Do the normal syntax would be something like this:

    Do myList.$sort($ref.myColumn,kFalse,$ref.anotherColumn,kFalse)

    or

    Do myList.$sort(myList.myColumn,kFalse,myList.anotherColumn,kFalse)

    I personally prefer $ref.

    This said you would be able to create your string including the “$ref.” part. But when executing you would need to make sure that the string is executed.

    Not tested though but I guess something of this might work:

    Do myList.$search([mySearchArgument])

    or maybe

    Do myList.$search(eval(mySearchArgument))

    Hope this helps.

    Best,

    Andreas

  • Andreas Pfeiffer

    Administrator
    January 31, 2026 at 11:52 am

    Uwe,

    On the other hand instead of using the real column names you could also do something like this. So if you know the list has two columns you could do this:

    Do myList.$search($ref.c1,kFalse,$ref.c2,kFalse)

    Best,

    Andreas

  • Uwe Smidt

    Member
    January 31, 2026 at 12:06 pm

    Hi Andreas,

    Thanks for your prompt response.

    I will try the [] and eval – approach, but your second answer does allow for the dynamic aspect of my intended method: I do not know how many sort column there will be, so a static approach will not work.

    Right now, I am trying to work around the problem (very unelegantly) with:

    Switch prSortByFields.$colcount ## depending on no of sort cols..

    Case 1

    Do llList.$sort($ref.[prSortByFields.1],kFalse) ## ...set up the $sort - params

    Case 2

    Do llList.$sort($ref.[prSortByFields.1],kFalse,$ref.[prSortByFields.2],kFalse)

    Case 3

    Do llList.$sort($ref.[prSortByFields.1],kFalse,$ref.[prSortByFields.2],kFalse,$ref.[prSortByFields.3],kFalse)

    Case 4

    Do llList.$sort($ref.[prSortByFields.1],kFalse,$ref.[prSortByFields.2],kFalse,$ref.[prSortByFields.3],kFalse,$ref.[prSortByFields.4],kFalse)

    Case 5

    Do llList.$sort($ref.[prSortByFields.1],kFalse,$ref.[prSortByFields.2],kFalse,$ref.[prSortByFields.3],kFalse,$ref.[prSortByFields.4],kFalse,$ref.[prSortByFields.5],kFalse)

    Default

    #0 or more than 5 sort coloumns: do nothing, as yet

    End Switch

    But I’ll try with [] and eval()!
    Best regards
    Uwe

  • Uwe Smidt

    Member
    January 31, 2026 at 12:27 pm

    [], eval() and evalf() did not work 😳, but my unelegant spaghetti code did work.

    I would still favour a more elegant solution than switch casing, because this dynamic problem not only applies to $sort, but everywhere where $ref is use in the parameter of a method, eg $search() etc

Log in to reply.