Multiple attributes sort on ListSort

Multiple attributes sort on ListSort

  

Hi.

How can I sort by multiple attributes on ListSort System Action?

I know that SortRecordList Extension could do it.

(Eg: "Entity1.attribute1,Entity1.attribute2,Entity2.attribute")

But I think ListSort has advantage because of the 'By' parameter is Genelic Type.

So we can change attribute name easier.


Best regards,

Hi Naomichi,

There is one solution, but it's not the best performance-wise.

You can run the ListSort action several times and everytime use the previous action output as input, but make sure you run the last order first.

Kind regards,

Remco Dekkinga

Hi Naomichi Baba,

   Why you are not creating dynamic order by  .you can pass the order by parameter dynamically.



Thanks

Rajendra Singh

Hi Remco,

Thanks for your reply.


As you mentioned, I tried to run the ListSort action several times.

(Note that, the ListSort action doesn't have any output parameter, so I put the same list to each input parameters.)

But It didn't work expected.

The list was sorted only by last run attribute and unsorted by other attributes.

Look at the result picture of my sample program, which I attached.


Regards,

Hi Rajendra,

Thanks for your reply.


In my understanding, dynamic order by is used in Aggregate.

But for example if we want to sort record list which sourced from JSON or Excel or input by users, I think we cannot use it.


Regards,

Naomichi Baba wrote:

Hi Remco,

Thanks for your reply.


As you mentioned, I tried to run the ListSort action several times.

(Note that, the ListSort action doesn't have any output parameter, so I put the same list to each input parameters.)

But It didn't work expected.

The list was sorted only by last run attribute and unsorted by other attributes.

Look at the result picture of my sample program, which I attached.


Regards,


Hi Naomichi, can you repost your attachment? I only see a bouncing ball with the text Processing Upload...


Hi Remco, thanks for notify to me that.
It seems there is something problem to attach a file.
I'll insert the image inline.

Hi Naomichi,

I'll dive into this issue and see what I can create. 

Kind regards,

Remco

Solution

Hi Naomichi,

Effectively ListSort sorts the whole list according to the criteria in By, so applying it multiple times will undo previous sorts and only consider the last one, since the underlying sorting algorithm is not stable.

Now, since you can write any expression in By, depending on the scenario you are able to produce an expression that uses all the attributes and achieves the sorting you want. In particular, if all your sort attributes are Texts with no spaces, like the case of Currency and Color, you can sort by both using an By expression Currency + "|" + Color.

Other data types are not so safe. For instance, if you had an integer Quantity attribute and used the same technique, it would be converted to a Text and the sorting order would be different. Say, if we had a list with the values 10, 2, 1 for Quantity and we sorted it By:

  1. Quantity, would result in: 1, 2, 10
  2. IntegerToText(Quantity), would result in: 1, 10, 2

I think that in the case of Dates, the sorting order is the same after converting to Text, as long as the 0 padding is kept for months and days (i.e. 2017-01-02 is ok, while 2017-1-2 is not).

So, bottom line, for that particular scenario you should be able to use the By expression OrderDate + "|" + Currency + "|" + Color.

Regards,

Paulo Ferreira

Solution

Hi Paulo,

I solved the issue. Thank you very much for reply!

Hello everyone

I was trying to implement the solution Paulo suggested, but for some reason, it's not working for me.

I'm  trying to sort a list by the Order attribute (this is a text field that concatenates the GUIDs of several records to build a tree-like list) and then by the record's Name. However, when I use the "|" separator to sort by more than one field, it appears to only sort by the first one.

Can you guys tell me what I'm doing wrong?

Thanks in advance


Best regards,

Ângelo Sousa

Hi Ângelo,

Can you give an example of the records in the list, and what you're observing as output using that solution?

Regards,

Paulo Ferreira

As another viable alternative (but not as modern as the ListSort system action), you can use the SortRecordList extension.

Also, feel free to vote on this idea I've created sometime ago, it proposes just what you want.

leonardo.fernandes wrote:

As another viable alternative (but not as modern as the ListSort system action), you can use the SortRecordList extension.

Also, feel free to vote on this idea I've created sometime ago, it proposes just what you want.

In one of the OutSystems courses, it was stated the SortRecordList extension shouldn't be used and should be replaced with ListSort?


Yes, Daryl. But, as I've stated on this idea, the ListSort system action cannot sort using multiple criteria. So, in this particular scenario, the SortRecordList remains the best option, at least in my opinion.

leonardo.fernandes wrote:

Yes, Daryl. But, as I've stated on this idea, the ListSort system action cannot sort using multiple criteria. So, in this particular scenario, the SortRecordList remains the best option, at least in my opinion.

I agree with you 100% Leonardo. I was disappointed that the ListSort action doesn’t mimic the Order By syntax of SQL as I would expect. I was just sharing that OutSystems recommends it. I’m actually giving some thought into addressing the issue. Your opinion is sound and respected.