ASP.NET / C#: Sort by NetProfit

How can I sort PositionsToClose by NetProfit?
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleSmartBotRobot : Robot
    {
        protected override void OnStart()
        {
            Positions PositionsToClose = Positions;
            foreach (Position p in PositionsToClose)
            {
                Print(Math.Abs(p.NetProfit));
            }
        }
    }
}

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You can probably use LINQ:

...

foreach (Position p in PositionsToClose.Orderby(item => item.NetProfit))
{
   ...

Open in new window

skijAuthor Commented:
Thanks, käµfm³d.  That is a good but it does not work.  Here is the error it returns:
Error CS1061: 'cAlgo.API.Positions' does not contain a definition for 'Orderby' and no extension method 'Orderby' accepting a first argument of type 'cAlgo.API.Positions' could be found (are you missing a using directive or an assembly reference?)

Here is the documentation for Positions:
http://ctdn.com/api/reference/positions

Here is the application:
http://spotware.ctrader.com/calgo-spotware-setup.exe
Faheem ShaikhCommented:
You can implement PositionsToClose as a Generic List of collections instead of an array and then extend it to implement the interface IComparable and override CompareTo method. Then use the Sort() method on your generic list to sort by any property you wish. In your case NetProfit.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sorting_a_List
{
    // a simple class to store in the list
    public class Employee : IComparable<Employee>
    {
        private int empID;

        public Employee(int empID)
        {
            this.empID = empID;
        }

        public override string ToString( )
        {
            return empID.ToString( );
        }

        public bool Equals(Employee other)
        {
            if (this.empID == other.empID)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // Comparer delegates back to Employee
        // Employee uses the integer's default
        // CompareTo method

        public int CompareTo(Employee rhs)
        {
            return this.empID.CompareTo(rhs.empID);
        }

    }
    public class Tester
    {
        static void Main( )
        {

            List<Employee> empList = new List<Employee>( );
            List<Int32> intList = new List<Int32>( );

            // generate random numbers for both the
            // integers and the employee IDs
            Random r = new Random( );

            // populate the list
            for (int i = 0; i < 5; i++)
            {
                // add a random employee id
                empList.Add(new Employee(r.Next(10) + 100));

                // add a random integer
                intList.Add(r.Next(10));
            }

            // display all the contents of the int list
            Console.WriteLine("List<int> before sorting:");
            for (int i = 0; i < intList.Count; i++)
            {
                Console.Write("{0} ", intList[i].ToString( ));
            }
            Console.WriteLine("\n");

            // display all the contents of the Employee list
            Console.WriteLine("List<Employee> before sorting:");
            for (int i = 0; i < empList.Count; i++)
            {
                Console.Write("{0} ", empList[i].ToString( ));
            }
            Console.WriteLine("\n");

            // sort and display the int list
            Console.WriteLine("List<int>after sorting:");
            intList.Sort( );
            for (int i = 0; i < intList.Count; i++)
            {
                Console.Write("{0} ", intList[i].ToString( ));
            }
            Console.WriteLine("\n");

            // sort and display the Employee list
            Console.WriteLine("List<Employee>after sorting:");

            //Employee.EmployeeComparer c = Employee.GetComparer( );
            //empList.Sort(c);

            empList.Sort( );

            // display all the contents of the Employee list
            for (int i = 0; i < empList.Count; i++)
            {
                Console.Write("{0} ", empList[i].ToString( ));
            }
            Console.WriteLine("\n");

        }
    }
}
The output looks something like this:
List<int> before sorting:
6 9 8 3 6
List<Employee> before sorting:
108 103 107 102 109
List<int>after sorting:
3 6 6 8 9
List<Employee>after sorting:
102 103 107 108 109

Open in new window

Faheem ShaikhCommented:
There are other ways of doing this as well like Using LINQ or reflection. Please review the below link:
http://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
I was able to get it to work with LINQ.  The B in OrderBy had to be capitalized!
foreach (Position p in PositionsToClose.OrderBy(item => item.NetProfit))

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.