How to reverse a list of records in Microflow?

0
Hi all, I am retrieving a list of records from data base( without sorting). Now I want to reverse the list. For example, retrieved list contains {B, D, E, A, C }. Now I want this list to be reversed like { C, A, E, D, B }. How can I do this in a microflow? Please give me an idea how I can approch this. Thanks in advance.   Thanks, Venkat.
asked
4 answers
1

Working solution for reversing any Objectlist using a microflow, recursively calling a sub_microflow.

This microflow https://modelshare.mendix.com/models/b3b43833-a034-446d-8332-2219eaba908a/objectlist-reverse

Recursively calls this sub_microflow:https://modelshare.mendix.com/models/097cafff-a866-469f-8347-dcf626957f7d/sub-reverse

Endresult will be that the OrdersReversed contain the same objectlist as Orders only reversed.

You can also checkout https://demo165.mxapps.io or in the app store MyReversingMicroflowWidget

 

answered
1

I got solution for this question by creating a java action which reverses the list. The below one is my Microflow. 

 

And here is the java code for java action.

 

// This file was generated by Mendix Modeler.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package test.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class ReverseInquiryProductList extends CustomJavaAction<java.lang.Boolean>
{
    private java.util.List<IMendixObject> __InputList;
    private java.util.List<texisle.proxies.InquiryProduct> InputList;
    private java.util.List<IMendixObject> __OutputList;
    private java.util.List<texisle.proxies.InquiryProduct> OutputList;

    public ReverseInquiryProductList(IContext context, java.util.List<IMendixObject> InputList, java.util.List<IMendixObject> OutputList)
    {
        super(context);
        this.__InputList = InputList;
        this.__OutputList = OutputList;
    }

    @Override
    public java.lang.Boolean executeAction() throws Exception
    {
        this.InputList = new java.util.ArrayList<texisle.proxies.InquiryProduct>();
        if (__InputList != null)
            for (IMendixObject __InputListElement : __InputList)
                this.InputList.add(texisle.proxies.InquiryProduct.initialize(getContext(), __InputListElement));

        this.OutputList = new java.util.ArrayList<texisle.proxies.InquiryProduct>();
        if (__OutputList != null)
            for (IMendixObject __OutputListElement : __OutputList)
                this.OutputList.add(texisle.proxies.InquiryProduct.initialize(getContext(), __OutputListElement));

        // BEGIN USER CODE
        try{
            if(__InputList !=null)
                for(int i=__InputList.size()-1;i>=0;i--){
                    __OutputList.add(__InputList.get(i));
                }
            return true;
        }
        catch(Exception e){
            return false;
        }
        
        
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @Override
    public java.lang.String toString()
    {
        return "ReverseInquiryProductList";
    }

    // BEGIN EXTRA CODE
    // END EXTRA CODE
}

 

Thanks,

Venkatesh.
 

answered
0

If you add an integer attribute 'sort', you could loop through the list and start with setting the first value to -1. Then subtract 1 from your counter, so the next item gets -2. Then, after the loop, you can (list operation) sort the list on the attribute 'sort', and it will place the lowest value first.

answered
0

No clean way yet, but what you can do is:

- create a parameter fill it with action list -head

- create a second parameter fill it with action list -tail

- pass those two to a submicroflow and with ActionList Union , 

See https://modelshare.mendix.com/models/0be6a4ef-925e-4fd9-a43f-51ab622def56/ac-t-order-sort for a start. You will have to make all steps after and including "Tail Empty" into a recursive calling subflow.

NB. With a big enough list, this will for sure give you performance issues, as recursive actions tend to take up memory and you wil be passing the same data down and up in your calls.

answered