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....
How to create a custom sort on category page?
How to create a custom sort on category page?
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
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
Re: How to create a custom sort on category page?
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
Now where ever you want to sort ProductCollection via Summary field you need to do following
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
}
Code: Select all
ProductCollection products = ProductDataSource.LoadForStore(); //Load all store products
products.Sort(new ProductComparer(SortDirection.Ascending)); // sort products
Re: How to create a custom sort on category page?
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!
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
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
Re: How to create a custom sort on category page?
Could you use this code to sort by CreatedDate?
Re: How to create a custom sort on category page?
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
}