Sort big data collection with IComparer

Posts   
 
    
doru
User
Posts: 2
Joined: 28-Jan-2008
# Posted on: 29-Jan-2008 09:50:42   

Hy, I have a question about if there would be another way to sort a collection that has lot's of data on a field that is on another Entity wich is in relation with my collection.The way I do it now is I am using a IComparer to sort.The problem is that is very slow...the way I am doing it now.I have also some code so you'd have a better undertanding how I am doing it. I am using Sql Server 2005 and LLBL GEN Pro 2.5.

DateOfLastStatusComparer dateOfLastStatusComparer = new DateOfLastStatusComparer();

orders.Sort((int)SalesOrderFieldIndex.SalesOrderId, (GridSortDirection == "ASC") ? System.ComponentModel.ListSortDirection.Ascending : System.ComponentModel.ListSortDirection.Descending, dateOfLastStatusComparer);

Where orers is a SalesOrderCollection -and the this collection is in relation with the SalesOrderLogEntity . And I needed to sort all the orders by the field LogDate in the SalesOrderLogEntity.

Here is the IComparer :
      public class DateOfLastStatusComparer:IComparer<object>
{
    #region IComparer Members

    public int Compare(object x, object y)
    {
        try
        {
            int orderId1, orderId2;
            int.TryParse(x.ToString(), out orderId1);
            int.TryParse(y.ToString(), out orderId2);
            SalesOrderEntity order1 = new SalesOrderEntity(orderId1);
            SalesOrderEntity order2 = new SalesOrderEntity(orderId2);
            if (order1.SalesOrderLog.Count > 0 && order2.SalesOrderLog.Count > 0)
                return DateTime.Compare(order1.SalesOrderLog[order1.SalesOrderLog.Count - 1].LogDate, order2.SalesOrderLog[order2.SalesOrderLog.Count - 1].LogDate);
            else
                return 0;
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    #endregion  
}

If you  know a better idea to do this, so it would more efecient please reaply me.Thanks in advance.
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Jan-2008 10:23:23   

I assume you are using SelfServicing. Don't re-instantiate the objects in the Compare method, which would fetch the entities from the database again, and fetch their related SalesOrderLog too.

If your original entities had the related SalesOrderLog loaded, then try the following code. If not then why don't you fetch the main collection sorted on the database side.

     public class DateOfLastStatusComparer:IComparer<object>
    {
        #region IComparer Members

        public int Compare(object x, object y)
        {
            try
            {
                SalesOrderEntity order1 = (SalesOrderEntity)x;
                SalesOrderEntity order2 = (SalesOrderEntity)y;
                if (order1.SalesOrderLog.Count > 0 && order2.SalesOrderLog.Count > 0)
                    return DateTime.Compare(order1.SalesOrderLog[order1.SalesOrderLog.Count - 1].LogDate, order2.SalesOrderLog[order2.SalesOrderLog.Count - 1].LogDate);
                else
                    return 0;
            }
            catch (Exception e)
            {
                return 0;
            }
        }

        #endregion  
    }
doru
User
Posts: 2
Joined: 28-Jan-2008
# Posted on: 29-Jan-2008 12:03:54   
   Thank you for your solution :) . It works faster now.