web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to get values in ascending order from list

(0) ShareShare
ReportReport
Posted on by 230
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. 
 
 
 
 
 
I have the same question (0)
  • GirishS Profile Picture
    27,827 Moderator on at
    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.
  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at
    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.
  • CU21091228-0 Profile Picture
    230 on at
    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.
  • Suggested answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at
    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();
            }
        }
    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans