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,


    <FONT face="Courier New">We cannot set the Icomparer directly for a column. However, we can create our own comparer class my implementing the S<FONT size=2>ystem.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. </FONT></FONT>


    <FONT face="Courier New">Next, we can create an array of <FONT size=2>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.</FONT></FONT>


    <FONT face="Courier New"><FONT size=2>We may then use the Sort method of SheetView and pass the StartRowIndex, StartColumnIndex, RowCount, ColumnCount, ByRows and the arry of SortInfo. </FONT></FONT>


    <FONT face="Courier New"><FONT size=2>Below is the code that can be used to apply the IComparer on single column. </FONT></FONT>

    <FONT face="Courier New"><FONT size=2><FONT color=#0000ff size=2><FONT color=#0000ff size=2>

    Public</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Class</FONT></FONT><FONT size=2> myStringComparer


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Implements</FONT></FONT><FONT size=2> System.Collections.IComparer


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Public</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Function</FONT></FONT><FONT size=2> Compare(</FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>ByVal</FONT></FONT><FONT size=2> x </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>As</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Object</FONT></FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>ByVal</FONT></FONT><FONT size=2> y </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>As</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Object</FONT></FONT><FONT size=2>) </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>As</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Integer</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Implements</FONT></FONT><FONT size=2> System.Collections.IComparer.Compare


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Return</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>New</FONT></FONT><FONT size=2> CaseInsensitiveComparer().Compare(y, x)


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>End</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Function


    End</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Class</FONT></FONT>


    <FONT color=#0000ff>''''Page load event.</FONT>

    <FONT color=#0000ff size=2><FONT color=#0000ff size=2><FONT size=2>

    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>Dim</FONT></FONT><FONT size=2> si(1) </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>As</FONT></FONT><FONT size=2> SortInfo


    si(0) = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>New</FONT></FONT><FONT size=2> SortInfo(0, </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>False</FONT></FONT><FONT size=2>)


    si(0).Comparer = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>New</FONT></FONT><FONT size=2> myStringComparer()


    FpSpread1.ActiveSheetView.Sort(0, 0, FpSpread1.ActiveSheetView.ColumnCount - 1, 1, </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>True</FONT></FONT><FONT size=2>, si)


    </FONT></FONT></FONT></FONT><FONT size=2>I hope it helps. </FONT>


    Regards,


    Harish</FONT>

  • 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