In Pivot Viewer v2, sorting of multivalued facets is not taken care of automatically (i.e. if you are binding items in the non-cxml way). I took the sample provided at the end Pivot Viewer’s documentation here and changed the Employee class to:

public class Employee
{
	public Employee(string firstname, string lastname, DateTime? birthDate, string department, string office, string title, string officePhone, string personalPhone)
	{
		FirstName = firstname;
		LastName = lastname;
		Birthdate = birthDate;
		Department = new List<string> { department, "D1", "D2", "D3", "D4", "D5" };
		Office = office;
		Title = title;
		OfficePhone = officePhone;
		PersonalPhone = personalPhone;
	}

	public string FirstName { get; set; }
	public string LastName { get; set; }
	public DateTime? Birthdate { get; set; }
	public List<string> Department { get; set; }
	public string Office { get; set; }
	public string Title { get; set; }
	public string OfficePhone { get; set; }
	public string PersonalPhone { get; set; }
}

i.e. the “Department” variable is now of List<string> type. Then, I added “Department” as a Filterable Pivot Property in the xaml like:

<sdk:PivotViewerStringProperty Id="Department" Options="CanFilter" DisplayName="Department" Binding="{Binding Department}" />

The result was when I tried to sort on this property, it threw an error:

“Failed to compare two elements in an array. At least one object must implement IComparable”

As the error explains, the error comes because the List object doesn’t have an implementation for IComparable due to which the sorting is not supported. It kind of makes sense too since a generic sorting criteria of how a List object is compared with another cant be decided. To make this work, I created a custom class that inherits the List object and has an implementation for the IComparable interface. For the sorting comparison, I did it according to the number of items in an object compared with the other. I know its not going to be correct in most of cases, but hey…its just a feasibility shown here, you can sophisticate the sorting mechanism as much as you like! 🙂 The custom class goes like:

/// <summary>
/// Custom class to support multivalued facets sorting
/// </summary>
public class StringFacets : List<string>, IComparable
{
	#region Implementation of IComparable

	/// <summary>
	/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
	/// </summary>
	/// <returns>
	/// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj"/> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj"/>.Greater than zero This instance follows <paramref name="obj"/> in the sort order.
	/// </returns>
	/// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception>
	public int CompareTo(object obj)
	{
		//PUT YOUR SORTING LOGIC HERE
		var stringFacet = obj as StringFacets;
		if (stringFacet == null)
			return -1;

		if (stringFacet.Count < this.Count)
			return -1;
		
		if (stringFacet.Count == this.Count)
			return 0;
		
		if (stringFacet.Count > this.Count)
			return 1;

		return -1;
	}

	#endregion
}

 

And the class is to be used as follows in the Employee class:

public StringFacets Department { get; set; }

 

Now, when you will try to sort the items based on the  “Department” category, it would work like a charm. The code is available at https://skydrive.live.com/embedicon.aspx/Blog/SLPV2_Sorting.rar?cid=e24ef727df20622d&sc=documents.

If you would like to share any improvement, its going to be awesome!

Advertisements