jaredec18

Untitled

Sep 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. Dear Student ,
  2.  
  3. As per the requirement submitted above , kindly find the below solution.
  4.  
  5. NOTE :Enter dates in mm/dd//yyyy format
  6.  
  7. 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".
  8.  
  9. Default.aspx :
  10.  
  11. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SortApplication.Default" %>
  12.  
  13. <!DOCTYPE html>
  14.  
  15. <html xmlns="http://www.w3.org/1999/xhtml">
  16. <head runat="server">
  17. <title></title>
  18. </head>
  19. <body>
  20. <form id="form1" runat="server">
  21. <%-- textbox for integers --%>
  22. Enter Integers :<asp:TextBox runat="server" ID="txtNumbers"></asp:TextBox>
  23. <%-- button to sort integers --%>
  24. <asp:Button runat="server" Text="Sort Integers" ID="btnSortIntegers" OnClick="btnSortIntegers_Click" /> <br />
  25. <%-- label to display sorted numbers --%>
  26. <asp:Label runat="server" ID="lblNumbers"></asp:Label>
  27. <br />
  28. <br />
  29. Enter Strings :
  30. <%-- textbox for Strings --%>
  31. <asp:TextBox runat="server" ID="txtStrings"></asp:TextBox>
  32. <%-- button to sort strings --%>
  33. <asp:Button runat="server" Text="Sort Strings" ID="btnSortStrings" OnClick="btnSortStrings_Click" /> <br />
  34. <%-- label to display sorted strings --%>
  35. <asp:Label runat="server" ID="lblStrings"></asp:Label>
  36. <br />
  37. <br />
  38. Enter Dates :
  39. <%-- textbox for Dates --%>
  40. <asp:TextBox runat="server" ID="txtDates"></asp:TextBox>
  41. <%-- button to sort dates --%>
  42. <asp:Button runat="server" Text="Sort Dates" ID="btnSortDates" OnClick="btnSortDates_Click" /> <br />
  43. <%-- label to display sorted dates --%>
  44. <asp:Label runat="server" ID="lblDates"></asp:Label>
  45.  
  46. </form>
  47. </body>
  48. </html>
  49.  
  50. ****************************
  51.  
  52. Default.aspx.cs :
  53.  
  54. //namespace
  55. using System;
  56. using System.Collections.Generic;
  57. using System.Linq;
  58. using System.Web;
  59. using System.Web.UI;
  60. using System.Web.UI.WebControls;
  61. //application namespace
  62. namespace SortApplication
  63. {
  64. public partial class Default : System.Web.UI.Page
  65. {
  66. //Sort Integers button click
  67. protected void btnSortIntegers_Click(object sender, EventArgs e)
  68. {
  69. //taking numbers entered by user
  70. string num = txtNumbers.Text;
  71. //spliting numbers based on comma and storing it in the string array
  72. string[] numArray = num.Split(',');
  73. //declaring integer array
  74. int[] intArray = new int[numArray.Length];
  75. //converting each element in the integer
  76. for (int i = 0; i < numArray.Length; i++)
  77. {
  78. intArray[i]=int.Parse(numArray[i]);//convert to integer
  79. }
  80. Array.Sort(intArray);//sort the array
  81. //display each number on the lable
  82. string output = "";
  83. for (int i = 0; i < intArray.Length; i++)
  84. {
  85. if(i!=intArray.Length-1)
  86. {
  87. output += intArray[i] + ",";//concate each array element
  88. }
  89. else
  90. {
  91. //used for last element in the array
  92. //because at last comma is not needed
  93. output += intArray[i];
  94. }
  95.  
  96. }
  97. //display sorted array on the lable
  98. lblNumbers.Text ="Sorted Integers : "+output;
  99.  
  100. }
  101. //Sort Strings button click
  102. protected void btnSortStrings_Click(object sender, EventArgs e)
  103. {
  104. //taking strings entered by user and spliting based on the comma
  105. string[] stringArray = txtStrings.Text.Split(',');
  106. Array.Sort(stringArray);//sort the array
  107. //display each string on the lable
  108. string output = "";
  109. for (int i = 0; i < stringArray.Length; i++)
  110. {
  111. if (i != stringArray.Length - 1)
  112. {
  113. output += stringArray[i] + ",";//concate each array element
  114. }
  115. else
  116. {
  117. //used for last element in the array
  118. //because at last comma is not needed
  119. output += stringArray[i];
  120. }
  121.  
  122. }
  123. //display sorted array on the lable
  124. lblStrings.Text = "Sorted Strings : " + output;
  125. }
  126. //Sort Dates button click
  127. protected void btnSortDates_Click(object sender, EventArgs e)
  128. {
  129. //taking dates entered by user
  130. string date = txtDates.Text;
  131. //spliting dates based on comma and storing it in the string array
  132. string[] strArray = date.Split(',');
  133. //declaring date array
  134. DateTime[] dateArray = new DateTime[strArray.Length];
  135. //converting each element in the date
  136. for (int i = 0; i < strArray.Length; i++)
  137. {
  138. //spliting each array element based on /
  139. string[] ddmmyy = strArray[i].Split('/');
  140. //string dt = DateTime.Now.ToString("MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo);
  141. //convert to date
  142. dateArray[i] = new DateTime(int.Parse(ddmmyy[2]), int.Parse(ddmmyy[0]), int.Parse(ddmmyy[1]));
  143. //dateArray[i]= Convert.ToDateTime(strArray[i]);
  144. }
  145. Array.Sort(dateArray);//sort the array
  146. //display each date on the lable
  147. string output = "";
  148. for (int i = 0; i < dateArray.Length; i++)
  149. {
  150. if (i != dateArray.Length - 1)
  151. {
  152. int year = dateArray[i].Year;//get year
  153. int month = dateArray[i].Month;//get month
  154. int day = dateArray[i].Day;//get day
  155. output += String.Format("{0}/{1}/{2}",month,day,year) + ",";//concate each array element
  156. }
  157. else
  158. {
  159. //used for last element in the array
  160. //because at last comma is not needed
  161. int year = dateArray[i].Year;//get year
  162. int month = dateArray[i].Month;//get month
  163. int day = dateArray[i].Day;//get day
  164. output += String.Format("{0}/{1}/{2}", month, day, year);//concate each array element
  165. }
  166.  
  167. }
  168. //display sorted array on the lable
  169. lblDates.Text = "Sorted Dates : " + output;
  170. }
  171. }
  172. }
  173.  
  174. ======================================================
  175.  
  176. Output : Run application using F5 and will get the screen as shown below
  177.  
  178. Screen 1 :Default.aspx
  179. https://media.cheggcdn.com/media/f62/f626add1-5c93-4028-a696-6bcfa5c7ff57/phpI9VQDC.png
  180. Screen 2 :Sorted integers , strings and dates
  181. https://media.cheggcdn.com/media/26a/26a042e7-07cc-48e4-a824-452546be5a0c/php7GoCih.png
Advertisement
Add Comment
Please, Sign In to add comment