Click here to Skip to main content
12,348,925 members (67,864 online)
Click here to Skip to main content
Add your own
alternative version

Stats

7.2K views
Posted

Sorting Strings based on the position of the block letter

, 5 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 morestrings  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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

piyush_singh
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
Question[My vote of 2] Inefficient re-invention of the wheel Pin
verence5-Jun-12 2:45
memberverence5-Jun-12 2:45 
AnswerResponse Pin
piyush_singh4-Jun-12 23:22
memberpiyush_singh4-Jun-12 23:22 
GeneralRe: Response Pin
Andreas Gieriet5-Jun-12 12:18
memberAndreas Gieriet5-Jun-12 12:18 
GeneralRe: Response Pin
piyush_singh5-Jun-12 16:55
memberpiyush_singh5-Jun-12 16:55 
GeneralRe: Response Pin
Andreas Gieriet5-Jun-12 19:53
memberAndreas Gieriet5-Jun-12 19:53 
GeneralRe: Response Pin
PIEBALDconsult6-Jun-12 3:39
memberPIEBALDconsult6-Jun-12 3:39 
GeneralRe: Response Pin
PIEBALDconsult6-Jun-12 3:41
memberPIEBALDconsult6-Jun-12 3:41 
AnswerRe: Response Pin
Andreas Gieriet6-Jun-12 6:01
memberAndreas Gieriet6-Jun-12 6:01 
GeneralRe: Response Pin
piyush_singh6-Jun-12 16:40
memberpiyush_singh6-Jun-12 16:40 
GeneralMy vote of 2 Pin
Paul Conrad4-Jun-12 16:37
memberPaul Conrad4-Jun-12 16:37 
GeneralUnclear Pin
PIEBALDconsult4-Jun-12 11:10
memberPIEBALDconsult4-Jun-12 11:10 
AnswerRe: Unclear Pin
piyush_singh4-Jun-12 23:29
memberpiyush_singh4-Jun-12 23:29 
GeneralRe: Unclear Pin
PIEBALDconsult5-Jun-12 3:35
memberPIEBALDconsult5-Jun-12 3:35 
SuggestionVery inefficient! Pin
Matt T Heffron4-Jun-12 10:24
memberMatt T Heffron4-Jun-12 10:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 5 Jun 2012
Article Copyright 2012 by piyush_singh
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid