Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

How to get values in ascending order from list

(0) ShareShare
ReportReport
Posted on by 171
Hi Team,
 
In DP class i am getting value from contract class.
 
In DP class i am using List.
 
In list having values like <'8', '7', '4', '3', '6', '2'> and storing those values in container.
 
Below code i am using.
 
ListEnumerator     listIterator;List               _id;Container   con;_id = Contract.parmPurchId();listIterator = _id.getEnumerator();while (listIterator.moveNext()){         con += listIterator.current();}
 
Currently in con value is storing ['8', '7', '4', '3', '6', '2'].
 
But i want in con value should be in as ascending order.
 
Please let me know how do i get the value in ascending order. 
 
 
 
 
 
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    How to get values in ascending order from list
    It's a strange requirement, but it definitely can be implemented.
     
    Regarding sorting, there are many ways how to do it. The following one utilizes Sort() method of System.Array class.
     
    Here is an attempt for a full implementation. There are some bugs in setFieldValuesFromArray() (which I don't have time to address at the moment), so you shouldn't simply use it, but it'll give you an idea. And the sorting works fine.
    internal final class Demo
    {
        MyTable myTable;
        static Array fieldIds;
    
        public static void main(Args _args)
        {
            new Demo().run();
        }
    
        static void typeNew()
        {
            fieldIds = new Array(Types::Integer);
            fieldIds.value(1, fieldNum(MyTable, Field1));
            fieldIds.value(2, fieldNum(MyTable, Field2));
            fieldIds.value(3, fieldNum(MyTable, Field3));
            fieldIds.value(4, fieldNum(MyTable, Field4));
            fieldIds.value(5, fieldNum(MyTable, Field5));
        }
    
        private void run()
        {
            List list = this.createList();
    
            System.Int32[] array = this.listToClrArray(list);
            System.Array::Sort(array);
    
            this.setFieldValuesFromArray(array);
        }
    
        private List createList()
        {
            List l = new List(Types::Integer);
    
            l.addEnd(1);
            l.addEnd(3);
            l.addEnd(5);
            l.addEnd(2);
            l.addEnd(4);
            l.addEnd(7);
            l.addEnd(8);
            l.addEnd(18);
            l.addEnd(12);
            l.addEnd(10);
            l.addEnd(11);
            l.addEnd(14);
            l.addEnd(13);
            l.addEnd(19);
    
            return l;
        }
    
        private System.Int32[] listToClrArray(List _list)
        {
            System.Int32[] array = new System.Int32[_list.elements()]();
            int i;
    
            ListEnumerator enumerator = _list.getEnumerator();
            while (enumerator.moveNext())
            {
                array.Set(i, enumerator.current());
                i++;
            }
    
            return array;
        }
    
        private void setFieldValuesFromArray(System.Int32[] _array)
        {
            int valueIdx;
            int colIdx;
            int rowIdx, previousRowIdx;
            int fieldCount = fieldIds.lastIndex();
    
            System.Collections.IEnumerator enumerator = _array.GetEnumerator();
            while (enumerator.MoveNext())
            {
                int value = enumerator.Current;
                valueIdx++;
    
                rowIdx = ((valueIdx - 1) div fieldCount) + 1;
    
                if (rowIdx != previousRowIdx)
                {
                    if (previousRowIdx)
                    {
                        myTable.insert();
                    }
    
                    myTable.clear();
                    previousRowIdx = rowIdx;
                }
    
                if (rowIdx div 2)
                {
                    colIdx = fieldCount - (valueIdx mod fieldCount);
                }
                else
                {
                    colIdx = (valueIdx mod fieldCount);
                }
    
                myTable.(fieldIds.value(colIdx)) = value;
            }
    
            // Save the last row, if any.
            if (rowIdx)
            {
                myTable.insert();
            }
        }
    }
  • CU21091228-0 Profile Picture
    CU21091228-0 171 on at
    How to get values in ascending order from list
    In List having below data .
     
    List
    1
    3
    5
    2
    4
    7
    8
    18
    12
    10
    11
    14
    13
    19
     
    In temporary table i am storing the data in zigzag.
    Below are the fields and data from list
     
    Field1   Field2    Field3    Field4     Field5
    1           3               5          2          4
    10         12            18         8          7
    11         14            13        19
     
    First 5 records from list is left to right.
    Second 5 records from list  is right to left.
    Third 5 records from list is left to right.
     
    I am using sorting in report using following link
     
    But not getting the data as per the ascending order.
  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    How to get values in ascending order from list in D365FO
    ListIterator and ListEnumerator will give you elements in the order in which they're stored in the list. There is nothing you can do about it. But nothing you can take and do whatever you want with them, such as sorting them.
     
    Nevertheless it's not clear what you want to with the data in your case. If you put values as records to a (temporary) table for reporting purposes, it doesn't matter in which order you do it. What matters is the sorting you use when presenting the data in your report.
  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    How to get values in ascending order from list in D365FO
    Hi,
     
    Can I know the reason of why you want the container value in ascending order?
    I think below blog will help you, but it will be time consuming one as it involves multiple "for loops".
    If you explain why, you want sorting, we will give you a better suggestion.
     
    Thanks,
    Girish S.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans