Sunday 20 September 2015

C# Name Sorting Program

Its is a simple C# program that sort the student name in ascending order. The program take the student name and grade and sort the name in ascending order.


Source Code 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sortStdNames
{
class Program
{
struct student
{
public string name;
public int id;
public char grade;
};
student[] stdInstance = new student[2];
int size;
static void Main(string[] args)
{
Program m = new Program();
m.size = 0;
welcomeScrean();
int j = 2;
Console.Write( "\t Enter Detail of 2 students " +"\n");
for (int i=0; i < j; i++)
{
getStudentsWithGrade(m);
}

displayAllStudents(m);
sortAndDisplayAllStudents(m);
String s = Console.ReadLine();
}

private static void sortAndDisplayAllStudents(Program m)
{

String[] temp= new String[m.size];
List<string> l = new List<string>();
for ( int h = 0 ; h<m.size ; h++ )
{
try
{
l.Add(m.stdInstance[h].name);
}
catch (IndexOutOfRangeException e){

}
}
l.Sort();
Console.Write("\n"
+ "\a"
+ "\t *******************************************************" + "\n"
+ "\t ** Sorted names are **" + "\n"
+ "\t *******************************************************" + "\n");
Console.Write("\t \n");
foreach (String c in l)
{
Console.WriteLine("\t"+c);
}
}

private static void displayAllStudents(Program m)
{
Console.Write("\n"
+ "\t Students added with following data" + "\n"
+ "\t ======================================" + "\n"
+ "\n");

for (int n = 0; n < m.size; n++)
{
try
{
Console.Write("\n\t Name: " + m.stdInstance[n].name + "\n"
+ "\t Grade: "+m.stdInstance[n].grade);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("IndexOutOfRangeException caught and " + e);
}
}
}

private static void getStudentsWithGrade(Program m)
{
Console.Write( "\n"
+"\a"
+"\t *******************************************************" +"\n"
+"\t ** Adding Student No # " +m.size +" **" +"\n"
+"\t *******************************************************" +"\n");

Console.Write( "\n"
+"\n\tEnter Name : ");


try
{
m.stdInstance[m.size].name = Console.ReadLine();

Console.Write( "\n\tEnter Grade ");

m.stdInstance[m.size].grade = Convert.ToChar(Console.ReadLine());//taking grade

m.stdInstance[m.size].id = m.size;//settind customer id
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("IndexOutOfRangeException caught and " + e);
}
m.size++;
Console.Write( "\n"
+"\t Press Enter to continue . . ." +"\n");
String s= Console.ReadLine();
}

private static void welcomeScrean()
{

}
}
}

Output of the Program

Sort student name according to their names

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.