Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dear Student ,
- As per the requirement submitted above , kindly find the below solution.
- NOTE :Enter dates in mm/dd//yyyy format
- Here a new Asp.net web Application in C# is created using Visual Studio 2017 with name "SortApplication".This application contains a web page with name "Default.aspx".
- Default.aspx :
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SortApplication.Default" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <%-- textbox for integers --%>
- Enter Integers :<asp:TextBox runat="server" ID="txtNumbers"></asp:TextBox>
- <%-- button to sort integers --%>
- <asp:Button runat="server" Text="Sort Integers" ID="btnSortIntegers" OnClick="btnSortIntegers_Click" /> <br />
- <%-- label to display sorted numbers --%>
- <asp:Label runat="server" ID="lblNumbers"></asp:Label>
- <br />
- <br />
- Enter Strings :
- <%-- textbox for Strings --%>
- <asp:TextBox runat="server" ID="txtStrings"></asp:TextBox>
- <%-- button to sort strings --%>
- <asp:Button runat="server" Text="Sort Strings" ID="btnSortStrings" OnClick="btnSortStrings_Click" /> <br />
- <%-- label to display sorted strings --%>
- <asp:Label runat="server" ID="lblStrings"></asp:Label>
- <br />
- <br />
- Enter Dates :
- <%-- textbox for Dates --%>
- <asp:TextBox runat="server" ID="txtDates"></asp:TextBox>
- <%-- button to sort dates --%>
- <asp:Button runat="server" Text="Sort Dates" ID="btnSortDates" OnClick="btnSortDates_Click" /> <br />
- <%-- label to display sorted dates --%>
- <asp:Label runat="server" ID="lblDates"></asp:Label>
- </form>
- </body>
- </html>
- ****************************
- Default.aspx.cs :
- //namespace
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- //application namespace
- namespace SortApplication
- {
- public partial class Default : System.Web.UI.Page
- {
- //Sort Integers button click
- protected void btnSortIntegers_Click(object sender, EventArgs e)
- {
- //taking numbers entered by user
- string num = txtNumbers.Text;
- //spliting numbers based on comma and storing it in the string array
- string[] numArray = num.Split(',');
- //declaring integer array
- int[] intArray = new int[numArray.Length];
- //converting each element in the integer
- for (int i = 0; i < numArray.Length; i++)
- {
- intArray[i]=int.Parse(numArray[i]);//convert to integer
- }
- Array.Sort(intArray);//sort the array
- //display each number on the lable
- string output = "";
- for (int i = 0; i < intArray.Length; i++)
- {
- if(i!=intArray.Length-1)
- {
- output += intArray[i] + ",";//concate each array element
- }
- else
- {
- //used for last element in the array
- //because at last comma is not needed
- output += intArray[i];
- }
- }
- //display sorted array on the lable
- lblNumbers.Text ="Sorted Integers : "+output;
- }
- //Sort Strings button click
- protected void btnSortStrings_Click(object sender, EventArgs e)
- {
- //taking strings entered by user and spliting based on the comma
- string[] stringArray = txtStrings.Text.Split(',');
- Array.Sort(stringArray);//sort the array
- //display each string on the lable
- string output = "";
- for (int i = 0; i < stringArray.Length; i++)
- {
- if (i != stringArray.Length - 1)
- {
- output += stringArray[i] + ",";//concate each array element
- }
- else
- {
- //used for last element in the array
- //because at last comma is not needed
- output += stringArray[i];
- }
- }
- //display sorted array on the lable
- lblStrings.Text = "Sorted Strings : " + output;
- }
- //Sort Dates button click
- protected void btnSortDates_Click(object sender, EventArgs e)
- {
- //taking dates entered by user
- string date = txtDates.Text;
- //spliting dates based on comma and storing it in the string array
- string[] strArray = date.Split(',');
- //declaring date array
- DateTime[] dateArray = new DateTime[strArray.Length];
- //converting each element in the date
- for (int i = 0; i < strArray.Length; i++)
- {
- //spliting each array element based on /
- string[] ddmmyy = strArray[i].Split('/');
- //string dt = DateTime.Now.ToString("MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo);
- //convert to date
- dateArray[i] = new DateTime(int.Parse(ddmmyy[2]), int.Parse(ddmmyy[0]), int.Parse(ddmmyy[1]));
- //dateArray[i]= Convert.ToDateTime(strArray[i]);
- }
- Array.Sort(dateArray);//sort the array
- //display each date on the lable
- string output = "";
- for (int i = 0; i < dateArray.Length; i++)
- {
- if (i != dateArray.Length - 1)
- {
- int year = dateArray[i].Year;//get year
- int month = dateArray[i].Month;//get month
- int day = dateArray[i].Day;//get day
- output += String.Format("{0}/{1}/{2}",month,day,year) + ",";//concate each array element
- }
- else
- {
- //used for last element in the array
- //because at last comma is not needed
- int year = dateArray[i].Year;//get year
- int month = dateArray[i].Month;//get month
- int day = dateArray[i].Day;//get day
- output += String.Format("{0}/{1}/{2}", month, day, year);//concate each array element
- }
- }
- //display sorted array on the lable
- lblDates.Text = "Sorted Dates : " + output;
- }
- }
- }
- ======================================================
- Output : Run application using F5 and will get the screen as shown below
- Screen 1 :Default.aspx
- https://media.cheggcdn.com/media/f62/f626add1-5c93-4028-a696-6bcfa5c7ff57/phpI9VQDC.png
- Screen 2 :Sorted integers , strings and dates
- https://media.cheggcdn.com/media/26a/26a042e7-07cc-48e4-a824-452546be5a0c/php7GoCih.png
Advertisement
Add Comment
Please, Sign In to add comment