Introduction
The following console program sorts a group of string
literals (that will be stored inside an array) on the basis of the position of the first occurrence of a block
letter, all other occurrence of the block letter inside that string
should be ignored. Only if the position of the block letters is
the same in two different strings
which are being compared,
then the strings
will be sorted on the basis of their ASCII orders of their block letter. For example, the string
AAcd
will be placed ahead of aAcd as in the first string
"A" is placed at index 1 but in the second string
"A" is placed at 2. However, if both the block letters are at the same
position like Abcd and Wxyz then the former will be sorted above the latter (based on the ASCII code of their block letters). If no block letter is found inside a string
say, abcd then that string
will be placed at the bottom. If two or more strings
don't have any block letter then they will also be
sorted on the basis of their ASCII codes.
Using the code
The code is defined below along with the adequate comments.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Team_Rankings
{
public class UserCode
{
public static string[] output;
public static int[,] ArrayCopy;
public static int[] CapsPosition;
public static void GetTeamOrder(int input1, string[] input2)
{
if (input1 > 0)
{
bool Found;
output = new string[input1];
ArrayCopy = new int[input1, 2];
CapsPosition = new int[input1];
char[] char1;
char[] char2;
for (int i = 0; i < input1; i++)
{
Found = false;
int stringLength = input2[i].Length;
for (int z = 0; z < stringLength; z++)
{
if (Char.IsUpper(input2[i], z))
{
int test = (int)input2[i][z];
ArrayCopy[i, 0] = test;
ArrayCopy[i, 1] = i;
CapsPosition[i] = z;
Found = true;
break;
}
}
if (!Found)
{
CapsPosition[i] = -1;
if (input2[i].Length > 0)
{
int z = 0;
while (z < stringLength)
{
int test = (int)input2[i][z];
if (test >= 65 && test <= 122)
{
ArrayCopy[i, 0] = test;
ArrayCopy[i, 1] = i;
Found = false;
break;
}
ArrayCopy[i, 0] = 1000;
ArrayCopy[i, 1] = i;
z++;
}
}
else
{
ArrayCopy[i, 0] = 1000;
ArrayCopy[i, 1] = i;
}
}
}
#region Sorting
int temp1000;
int temp999;
int temp998;
for (int i = 0; i < input1; i++)
{
for (int z = input1 - 1; z >= 0; z--)
{
if ((CapsPosition[i] == CapsPosition[z]) || CapsPosition[i] == -1 || CapsPosition[z] == -1)
{
if ((ArrayCopy[i, 0] > ArrayCopy[z, 0] && ArrayCopy[i, 1] < ArrayCopy[z, 1]) ||
(ArrayCopy[i, 0] < ArrayCopy[z, 0] && ArrayCopy[i, 1] > ArrayCopy[z, 1]))
{
temp1000 = ArrayCopy[i, 1];
ArrayCopy[i, 1] = ArrayCopy[z, 1];
ArrayCopy[z, 1] = temp1000;
i = -1;
break;
}
else if (ArrayCopy[i, 0] == ArrayCopy[z, 0] && i != z)
{
int tempLength = (input2[i].Length < input2[z].Length) ? (input2[i].Length) : (input2[z].Length);
char1 = input2[i].ToCharArray();
char2 = input2[z].ToCharArray();
for (int a = 0; a < tempLength; a++)
{
temp999 = (char1[a] > 96) ? char1[a] : (char1[a] + 32);
temp998 = (char2[a] > 96) ? char2[a] : (char2[a] + 32);
if (temp999 > temp998)
{
if (ArrayCopy[i, 1] < ArrayCopy[z, 1])
{
temp1000 = ArrayCopy[i, 1];
ArrayCopy[i, 1] = ArrayCopy[z, 1];
ArrayCopy[z, 1] = temp1000;
z = input1;
break;
}
else
{
break;
}
}
else if (temp999 < temp998)
{
if (ArrayCopy[i, 1] > ArrayCopy[z, 1])
{
temp1000 = ArrayCopy[i, 1];
ArrayCopy[i, 1] = ArrayCopy[z, 1];
ArrayCopy[z, 1] = temp1000;
z = input1;
break;
}
else
{
break;
}
}
}
}
}
else
{
if (((CapsPosition[i] > CapsPosition[z] && ArrayCopy[i, 1] < ArrayCopy[z, 1]) ||
CapsPosition[i] < CapsPosition[z] && ArrayCopy[i, 1] > ArrayCopy[z, 1]) &&
CapsPosition[i] >= 0 && CapsPosition[z] >= 0)
{
temp1000 = ArrayCopy[i, 1];
ArrayCopy[i, 1] = ArrayCopy[z, 1];
ArrayCopy[z, 1] = temp1000;
}
}
}
}
#endregion
for (int i = 0; i < input1; i++)
{
output[ArrayCopy[i, 1]] = input2[i];
}
}
else
{
output = new string[0];
}
foreach (string abc in output)
{
Console.WriteLine(abc);
}
Console.ReadLine();
}
static void Main(string[] args)
{
string[] abc = { "wZa", "wwwWa", "wbwwwa", "wWe", "sSsssssw",
"Aww", "abwqA","aXcd","kolkataKnight","aXcD" };
GetTeamOrder(10, abc);
}
}
}
Points of Interest
This is a very unique case and may not be applicable for simple sorting of data. The good thing about this code is that
I was able to deliver the output without help from anybody. It's always nice to complete your first complex project.