Step 1: Design Page – SortArray.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SortArray.aspx.cs" Inherits="SortArray" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sort Array Elements Without Built-in Methods</title>
<style>
body {
font-family: Arial;
background-color: #f0f2f5;
margin: 50px;
}
.container {
width: 520px;
margin: auto;
background: white;
border-radius: 8px;
box-shadow: 0px 0px 10px #ccc;
padding: 25px;
}
h2 {
color: #1A2A80;
text-align: center;
}
.form-control {
width: 100%;
padding: 8px;
margin-top: 10px;
}
.btn {
background-color: #7A85C1;
color: white;
border: none;
padding: 10px;
margin-top: 10px;
border-radius: 5px;
cursor: pointer;
width: 100%;
}
.result {
margin-top: 20px;
font-weight: bold;
color: #333;
text-align: center;
white-space: pre-line;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Sort Array Elements (Without Built-in Methods)</h2>
<asp:Label ID="lblInput" runat="server" Text="Enter Numbers (comma-separated):"></asp:Label><br />
<asp:TextBox ID="txtNumbers" runat="server" CssClass="form-control" placeholder="Example: 5,3,8,1,2"></asp:TextBox><br />
<asp:Button ID="btnSort" runat="server" Text="Sort Array" CssClass="btn" OnClick="btnSort_Click" /><br />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – SortArray.aspx.cs
using System;
using System.Linq;
public partial class SortArray : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSort_Click(object sender, EventArgs e)
{
string input = txtNumbers.Text.Trim();
if (string.IsNullOrEmpty(input))
{
lblResult.Text = "Please enter numbers separated by commas.";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
try
{
// Convert comma-separated string to integer array
int[] numbers = input.Split(',').Select(n => Convert.ToInt32(n.Trim())).ToArray();
// Manual Sorting (Bubble Sort logic)
for (int i = 0; i < numbers.Length - 1; i++)
{
for (int j = i + 1; j < numbers.Length; j++)
{
if (numbers[i] > numbers[j])
{
// Swap values
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
lblResult.Text = $"Sorted Array: {string.Join(", ", numbers)}";
lblResult.ForeColor = System.Drawing.Color.Green;
}
catch
{
lblResult.Text = "Invalid input! Please enter only numbers separated by commas.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
}
Real-Time Example Flow
Open SortArray.aspx in your browser.
Enter numbers like:
5, 3, 8, 1, 2
Click “Sort Array”.
Output will be:
Sorted Array: 1, 2, 3, 5, 8
Explanation
| Step | Description |
|---|
| 1 | User enters numbers separated by commas. |
| 2 | Numbers are split and converted into an integer array. |
| 3 | The code uses nested loops (Bubble Sort) to compare and swap elements. |
| 4 | Sorted array is displayed in ascending order. |
Example Input / Output Table
| Input | Output |
|---|
| 10, 5, 2, 8, 1 | 1, 2, 5, 8, 10 |
| 9, 4, 7, 2 | 2, 4, 7, 9 |
| 100, 25, 50, 75 | 25, 50, 75, 100 |
| 3, 3, 2, 1 | 1, 2, 3, 3 |