Enter some words seperated by a Space, and this will sort them. If you do not want it be in alphabetical order, do not check the checkbox. If you want it in alphabetical order, check the check box.





Front end code

    5                 Enter some words seperated by a Space, and this will sort them. If you do not want it be in

    6                 alphabetical order, do not check the checkbox. If you want it in alphabetical order, check the check

    7                 box.

    8                 <br />

    9                 <asp:TextBox ID="txtValue" style="width:300px;height:200px;" TextMode="multiLine" runat="server" />

   10                 <asp:Button ID="btnSort" runat="server" Text="Sort" OnClick="btnSort_Click" />

   11                 <br />

   12                 <asp:CheckBox ID="chkAlpha" Text="Alphabetical Order" runat="server" />

   13                 <br />

   14                 <asp:Label ID="lblResults" runat="server" style="display:block;width:100%;height:200px;border:solid 1px;" />




Source Code.

    1 using System;

    2 using System.Data;

    3 using System.Configuration;

    4 using System.Collections;

    5 using System.Web;

    6 using System.Web.Security;

    7 using System.Web.UI;

    8 using System.Web.UI.WebControls;

    9 using System.Web.UI.WebControls.WebParts;

   10 using System.Web.UI.HtmlControls;

   11 namespace BicNet.Projects.mcp

   12 {

   13     public partial class StringSort : System.Web.UI.Page

   14     {

   15         protected void Page_Load(object sender, EventArgs e)

   16         {

   17 

   18         }

   19         protected void btnSort_Click(object sender, EventArgs e)

   20         {

   21             if(chkAlpha.Checked)

   22                 lblResults.Text = Strings.Sort(txtValue.Text);

   23             else

   24                 lblResults.Text = Strings.Sort(txtValue.Text,false);

   25         }

   26 }

   27 

   28     public class Strings

   29     {

   30         public static string Sort(string strText, bool AlphaOrder)

   31         {

   32             //Declare and Initialize a string array

   33             string[] strArray = {""};

   34             char[] chrSep = {' '};

   35             //Convert the string to an Array unsing System.String

   36             strArray = strText.Split(chrSep);

   37             //Use System.Array to sort

   38             System.Array.Sort(strArray);

   39             //If it's not alphabetic order, reverse the array

   40             if (!AlphaOrder)

   41             {

   42                 //User System.Array to reverse

   43                 System.Array.Reverse(strArray);

   44             }

   45             //REturn the string

   46             return System.String.Join(" ", strArray);

   47 

   48         }

   49 

   50         public static string Sort(string strText)

   51         {

   52             return Sort(strText, true);

   53         }

   54     }

   55 }