I have to write an algorithm to sort the names according to the second parameter, I am not able to get the logic how to do , the input file will contain data like this :
Jervie, 12, M , Jaimy ,11, F, Tony , 23, M ,Janey , 11, F
And the output has to be like this:
Jaimy, 11, F , Janey, 11, F, Jervie ,12, M, Tony , 23, M
They are sorted on the basis of second parameter, I have read this file and stored the data in struct array:
struct element
{
public string name;
public int numComp;
public string sex;
} ;
Now i am thinking of doing sorting numComp
, But how to process for algo , that's what i am not able to understand , Could some one help me please ?, If you know any other optimal solution then please do not hesitate to give, Thanks
My full code for what i done yet is :
class Program
{
struct element
{
public string name;
public string sex;
public int numComp;
} ;
static void Main(string[] args)
{
string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
string text = System.IO.File.ReadAllText(appRootDir+"\\File.txt");
string[] words = text.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int counter = 0 ,pos=0;
element[] storage = new element[words.Count()/3];
foreach(string str in words)
{
if(counter==0)
{
storage[pos].name = str;
counter++;
}
else if (counter == 1)
{
storage[pos].numComp = Int32.Parse(str);
counter++;
}
else if(counter==2)
{
storage[pos].sex = str;
counter = 0; pos++;
}
}
//Now how to sort on the basis of numComp and print the output as desired ?
Console.ReadKey();
}
}
Now how to sort on the basis of numComp and print the output as desired ?
int[] arr = { 5, 1, 6, 3, 4, 5, 8, 3, 9, 2, 6, 7 }
?left[0] < right[0]
you will have `Comare(left[0], right[0]) < 0'