How to create a custom sort on category page?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

How to create a custom sort on category page?

Post by AbleMods » Thu Oct 01, 2009 2:16 pm

I'm using CategoryGridPage3 for a nice page setup and it works great.

But I need the products displayed to sort by a value stored in the product Summary field. Is it possible to modify what fields are used for sorting on the categorygridpage controls? I saw some comparer stuff in the code but I'm not all that familiar with comparers....
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: How to create a custom sort on category page?

Post by mazhar » Fri Oct 02, 2009 3:35 am

In order to sort either you need to adjust and execute some query to bring results in desired order or alternatively perform sorting upon collections using comparer approach. In order to do this first you need your collection is extended from SortableCollectioin, in your case its already done. Next you need to implement a comparer to sort collection depending upon on desired field. For example for product' summary field it could be something like

Code: Select all

public class ProductComparer : IComparer
{
    SortDirection _SortDirection;
    public ProductComparer(SortDirection sortDirection)
    {
        _SortDirection = sortDirection;
    }

    #region IComparer Members
    public int Compare(object x, object y)
    {
        if (_SortDirection == SortDirection.Ascending)
            return ((Product)x).Summary.CompareTo(((Product)y).Summary);
        return ((Product)y).Summary.CompareTo(((Product)x).Summary);
    }
    #endregion
}
Now where ever you want to sort ProductCollection via Summary field you need to do following

Code: Select all

ProductCollection products = ProductDataSource.LoadForStore(); //Load all store products
products.Sort(new ProductComparer(SortDirection.Ascending));  // sort products

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: How to create a custom sort on category page?

Post by AbleMods » Fri Oct 02, 2009 8:27 am

That worked perfectly!

One interesting note. The ProductDataSource.NarrowSearch() method used in the categorygrid controls doesn't return a product collection. It returns a generic <list>Product instead.

So the iComparer used in this example didn't work - the parameters aren't cross compatible. My solution was simply to load the <list>Products into a new ProductCollection class and sort it that way. It worked exactly as a I needed - thanks so much Mazhar!

Code: Select all

        // pull in products into generic list
        List<Product> _ProductList = ProductDataSource.NarrowSearch(_Keywords, this.CategoryId, _ManufacturerId, 0, 0, _PageSize, (_HiddenPageIndex * _PageSize), SortResults.SelectedValue);

        // convert generic list to dataclass collection so it can be sorted
        ProductCollection _PList = new ProductCollection();
        foreach (Product _PLProduct in _ProductList)
            _PList.Add(_PLProduct);

        _PList.Sort(new ProductComparer(SortDirection.Ascending));  

        //ProductList.DataSource = ProductDataSource.NarrowSearch(_Keywords, this.CategoryId, _ManufacturerId, 0, 0, _PageSize, (_HiddenPageIndex * _PageSize), SortResults.SelectedValue);
        ProductList.DataSource = _PList;
        
        ProductList.DataBind();
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

pgdlc
Ensign (ENS)
Ensign (ENS)
Posts: 5
Joined: Mon Apr 16, 2012 2:52 pm

Re: How to create a custom sort on category page?

Post by pgdlc » Tue Feb 19, 2013 3:28 pm

Could you use this code to sort by CreatedDate?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: How to create a custom sort on category page?

Post by mazhar » Wed Feb 20, 2013 3:44 am

I think it will but with sime modifications to compare logic. Somthing like this may work

Code: Select all

public class ProductComparer : IComparer
{
    SortDirection _SortDirection;
    public ProductComparer(SortDirection sortDirection)
    {
        _SortDirection = sortDirection;
    }

    #region IComparer Members
    public int Compare(object x, object y)
    {
        if (x == null)
            return (y == null) ? 0 : 1;

        if (y == null)
            return -1;

        return ((DateTime)x).CompareTo((DateTime)y);
    }
    #endregion
}

Post Reply