Sorting Strings based on the position of the block letter






2.50/5 (4 votes)
Strings literals are sorted on the basis of the position of the first occurrence of a block letter inside it
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;
//CapsPosition will store the position of the block letter. Default value will be -1
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;
// Check the position of the 1st Block Letter
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];
//check if the letter is not a special character
if (test >= 65 && test <= 122)
{
//store the first literal of the string
//which do not contain any capital letter
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++)
{
//Found = false;
//if (ArrayCopy[i, 0] < 65)
//{
// //break;
//}
for (int z = input1 - 1; z >= 0; z--)
{
//For strings whose block letters are at a different
//position or they don't contain a block letter
//check if the string with the smaller char value is placed at the top or not
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;
//Found = true;
//i = (i > 0) ? -1 : i;
i = -1;
break;
}
//For strings whose block letters are at the same position
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)
{
//check if the greater value is placed ahead
if (ArrayCopy[i, 1] < ArrayCopy[z, 1])
{
temp1000 = ArrayCopy[i, 1];
ArrayCopy[i, 1] = ArrayCopy[z, 1];
ArrayCopy[z, 1] = temp1000;
//i = (i > 0) ? -1 : i;
z = input1;
//i = -1;
//Found = true;
break;
}
else
{
break;
}
}
else if (temp999 < temp998)
{
//check if the greater value is placed ahead
if (ArrayCopy[i, 1] > ArrayCopy[z, 1])
{
temp1000 = ArrayCopy[i, 1];
ArrayCopy[i, 1] = ArrayCopy[z, 1];
ArrayCopy[z, 1] = temp1000;
//i = (i > 0) ? -1 : i;
z = input1;
//i = -1;
//Found = true;
break;
}
else
{
break;
}
}
/*else
{
break;
}*/
}
}
}
else
{
//check if swapping is reqired
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++)
{
//Store the output
output[ArrayCopy[i, 1]] = input2[i];
}
}
else
{
output = new string[0];
//output[0] = "";
}
foreach (string abc in output)
{
//Display Output
Console.WriteLine(abc);
}
Console.ReadLine();
}
static void Main(string[] args)
{
//Sample Data for testing
string[] abc = { "wZa", "wwwWa", "wbwwwa", "wWe", "sSsssssw",
"Aww", "abwqA","aXcd","kolkataKnight","aXcD" };
//string[] abc = { "wwwwy","sssSsssw","abc" };
GetTeamOrder(10, abc);
//string[] abc = { "CbA" };
//GetTeamOrder(1, 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.