In: Computer Science
C# ( asp.net ) 2019 Visual Studio
I have a dropdown where you can select ( integer, string, date )
After selecting the desired dropdown option, user can input a list of inputs. For example; for integer: 2,5,7,9,1,3,4
And then , I have a 'sort' button
Can you please help me with the code behind for sorting them( For integer, string, and date )
Thank you.
In visual studio web application is created with name "SortInputs".Below are the details.
Default.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SortInputs.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<%-- title for web page --%>
<title>Sorting application</title>
</head>
<body>
<form id="form1" runat="server">
<%-- drop down list for data types --%>
Select Data Type :<asp:DropDownList ID="ddlDataTypes"
runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlDataTypes_SelectedIndexChanged">
<asp:ListItem Selected="True">--Select Data
type--</asp:ListItem>
<asp:ListItem>Integer</asp:ListItem>
<asp:ListItem>String</asp:ListItem>
<asp:ListItem>Date</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<%-- label to display list header --%>
<asp:Label ID="lblList"
runat="server"></asp:Label>
<%-- textbox for input lists --%>
<asp:TextBox runat="server" ID="txtList"
Visible="False"></asp:TextBox>
<%-- button to sort inputs --%>
<asp:Button runat="server" Text="Sort" ID="btnSort"
OnClick="btnSort_Click" Visible="False" />
<br />
<br />
<%-- label to display sorted inputs --%>
<asp:Label runat="server"
ID="lblHeader"></asp:Label>
</form>
</body>
</html>
******************************
Default.aspx.cs :
//namespace
using System;
//application namespace
namespace SortInputs
{
public partial class Default : System.Web.UI.Page
{
//When data type from dropdown list is selected
protected void ddlDataTypes_SelectedIndexChanged(object sender,
EventArgs e)
{
if (ddlDataTypes.SelectedIndex == 0)
{
//if defaule option is selected
lblHeader.Text = "Select valid data type";
}
else if (ddlDataTypes.SelectedIndex == 1)
{
//if integer data type is selected then
lblList.Text = "Enter Integers : ";//set text on the label
txtList.Visible = true;//show the textbox
btnSort.Visible = true;//show sort button
}
else if (ddlDataTypes.SelectedIndex == 2)
{
//if string data type is selected then
lblList.Text = "Enter Strings : ";//set text on the label
txtList.Visible = true;//show the textbox
btnSort.Visible = true;//show sort button
}
else if (ddlDataTypes.SelectedIndex == 3)
{
//if date data type is selected then
lblList.Text = "Enter Dates (dd/mm/yyyy) : ";//set text on the
label
txtList.Visible = true;//show the textbox
btnSort.Visible = true;//show sort button
}
}
//sort button click
protected void btnSort_Click(object sender, EventArgs e)
{
//taking input entered by user in the textbox
string inputList = txtList.Text;
//checking which data type is selected
if(ddlDataTypes.SelectedIndex == 1)
{
//if data type is integer
//call method to sort the integers
sortIntegers(inputList);
}
//checking which data type is selected
else if (ddlDataTypes.SelectedIndex == 2)
{
//if data type is string
//call method to sort the strings
sortStrings(inputList);
}
//checking which data type is selected
else if (ddlDataTypes.SelectedIndex == 3)
{
//if data type is date
//call method to sort the dates
sortDates(inputList);
}
}
//method to sort integers
public void sortIntegers(string intList)
{
//spliting intList based on comma (,)
string[] intArr = intList.Split(',');
int[] intSortArray = new int[intArr.Length]; //declaring integer
array to hold integers
//using for loop to loop through each array element and converting
each element in the integer
for (int j= 0;j < intArr.Length; j++)
{
intSortArray[j] = int.Parse(intArr[j]);//convert each array element
to integer
}
//calling Array Sort() method to sort the integer array
Array.Sort(intSortArray);
string integers = "";//display each number on the lable
//using for loop display each array in the label
for (int j = 0; j < intSortArray.Length; j++)
{
if (j != intSortArray.Length - 1)
{
integers += intSortArray[j] + ",";
}
else
{
integers += intSortArray[j];
}
}
lblHeader.Text =integers;//display integers
}
//method to Sort Strings
public void sortStrings(string stringList)
{
string[] sortedStrings = stringList.Split(',');//split input based
on ,
//sort the array using Array.Sort() method
Array.Sort(sortedStrings);
//concate sorted strings from array and display
string strings = "";
for (int i = 0; i < sortedStrings.Length; i++)
{
if (i != sortedStrings.Length - 1)
{
strings += sortedStrings[i] + ",";
}
else
{
strings += sortedStrings[i];
}
}
lblHeader.Text = strings;//display strings
}
//method to Sort Dates
public void sortDates(string dateList)
{
string[] dateArray = dateList.Split(',');//spliting input based on
,
//declaring date array to hold the dates
DateTime[] SortedDates = new DateTime[dateArray.Length];
//using for loop to converting each element of the array in the
date
for (int i = 0; i < dateArray.Length; i++)
{
string[] dmy = dateArray[i].Split('/');//split array element on
'/'
//usin above array form a date and store in the array
SortedDates[i] = new DateTime(int.Parse(dmy[2]), int.Parse(dmy[1]),
int.Parse(dmy[0]));
}
Array.Sort(SortedDates);//sort the array using Array.Sort()
string dates = "";//variable to concate each date
for (int i = 0; i < SortedDates.Length; i++)
{
int y = SortedDates[i].Year;//get year from the date
int m = SortedDates[i].Month;//get month from the date
int d = SortedDates[i].Day;//get day from the date
if (i != dateArray.Length - 1)
{
//concate day,month and year to form a date
dates += String.Format("{0}/{1}/{2}", d, m, y) + ",";
}
else
{
dates += String.Format("{0}/{1}/{2}", d, m, y);
}
}
lblHeader.Text = dates;//display dates
}
}
}
====================================
Screen 1:Default.aspx
Screen 2:Sorted integers
Screen 3:Sorted strings
Screen 4:Sorted dates