Setting the IComparer for a column

Posted by: kozu on 8 September 2017, 1:46 pm EST

  • Posted 8 September 2017, 1:46 pm EST

    Hi,

    Is there a way to set an IComparer for a specific column? My grid has text, numbers and dates in it and it would be easier to hold the comparers inside the grid rather than having a separate  hash with column index and comparer.

     thanx

  • Replied 8 September 2017, 1:46 pm EST

    Hello Kozu,


    We cannot set the Icomparer directly for a column. However, we can create our own comparer class my implementing the System.Collections.IComparer interface and implement the Compare method to implement the sorting logic. We may create separate classes for separte data types like string, number, dates etc.


    Next, we can create an array of FarPoint.Web.Spread.SortInfo in which we can specify the column or row to implement the sort and the sort order. For each array element we can set the comparer to the instance of our own comparer class.


    We may then use the Sort method of SheetView and pass the StartRowIndex, StartColumnIndex, RowCount, ColumnCount, ByRows and the arry of SortInfo.


    Below is the code that can be used to apply the IComparer on single column.


    Public Class myStringComparer


    Implements System.Collections.IComparer


    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare


    Return New CaseInsensitiveComparer().Compare(y, x)


    End Function


    End Class


    ''''Page load event.


    Dim si(1) As SortInfo


    si(0) = New SortInfo(0, False)


    si(0).Comparer = New myStringComparer()


    FpSpread1.ActiveSheetView.Sort(0, 0, FpSpread1.ActiveSheetView.ColumnCount - 1, 1, True, si)


    I hope it helps.


    Regards,


    Harish

  • Replied 8 September 2017, 1:46 pm EST

    Thanks Harish!

    I have a second question now: is there a way to trigger a sort in the underlying DataSource as well?

    thanx

     

  • Replied 8 September 2017, 1:46 pm EST

    Hello,


    Why do you want to sort the underlying DataSource? Sorting the Spread would be much faster and all the links back to the original datarows are intact, so there is nothing special you need to do to save, delete, etc. a record. If you want to sort the underlying DataSource, you would need to get it as a DataView and the DataView has a Sort property.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels