Icompare inheritance, compare two spfolder

Source: Internet
Author: User

In the past, I also implemented the inheritance of the icompare interface to compare two objects, but I forgot over time. Now I can see that I want to record it again so that I can review it later!

The followingCodeThis is an excerpt from the book Jian Yi liangjian, which is used to compare spfolder in Sharepoint. The following code implements two comparisons: Name, one is to use another property value (sorting number) for comparison. The Code is as follows.

To better understand this point, we use methods for sort and inherit the icompare class for sorting.

         ///   <Summary>  
/// Obtains the list of subfolders sorted by name.
/// </Summary>
/// <Param name = "Folders"> </param>
/// <Returns> </returns>
Static Public Ilist <spfolder> getsortedfolders (spfoldercollection folders)
{
List <spfolder> sortedfolders = New List <spfolder> ();

Foreach (Spfolder F In Folders)
{
If (F. item! =Null ) // Non-system folder
Sortedfolders. Add (f );
}

Sortedfolders. Sort (comparefolder );

Return Sortedfolders;
}

Static Int Comparefolder (spfolder F1, spfolder F2)
{
Return F1.name. compareto (f2.name );
}

/// <Summary>
/// Obtain the subfolders sorted by a field.
/// </Summary>
/// <Param name = "Folders"> </param>
/// <Param name = "sortfieldname"> </param>
/// <Returns> </returns>
Static Public Ilist <spfolder> getsortedfolders (spfoldercollection folders, String Sortfieldname)
{

List <spfolder> sortedfolders = New List <spfolder> ();

Foreach (Spfolder F In Folders)
{
If (F. item! = Null ) // Non-system folder
Sortedfolders. Add (f );
}

Spfoldercomparer c = New Spfoldercomparer (sortfieldname );

Sortedfolders. Sort (C );

Return Sortedfolders;
}

Class Spfoldercomparer: icomparer <spfolder>
{
Private String _ Sortfieldname;
Public Spfoldercomparer ( String Sortfieldname)
{
_ Sortfieldname = sortfieldname;
}

# Region Icomparer <spfolder> Member

Public Int Compare (spfolder X, spfolder y)
{
// If (ishiddenfolder (x) | ishiddenfolder (y) return 0;

If (! X. item. Fields. containsfield (_ sortfieldname) |! Y. item. Fields. containsfield (_ sortfieldname ))
Return 0 ;

String SX = "" + X. item [_ sortfieldname];
If (SX = "" )
SX = " 0 " ;

String Sy = "" + Y. item [_ sortfieldname];
If (SY = "" )
Sy = " 0 " ;

Return Convert. toint32 (SX). compareto (convert. toint32 (SY ));

}

# Endregion
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.