1

seriously need some guideline on string sorting methodology. Perhaps, if able to provide some sample code would be a great help. This is not a homework. I would need this sorting method for concurrently checking multiple channel names and feed the channel accordingly based on the sort name/string result.

Firstly I would have the string array pattern something like below:

string[] strList1 = new string[] {"TDC1ABF", "TDC1ABI", "TDC1ABO" };

string[] strList2 = new string[] {"TDC2ABF", "TDC2ABI", "TDC2ABO"};

string[] strList3 = new string[] {"TDC3ABF", "TDC3ABO","TDC3ABI"}; //2nd and 3rd element are swapped

I would like to received a string[] result like below:

//result1 = "TDC1ABF , TDC2ABF, TDC3ABF"
//result2 = "TDC1ABI , TDC2ABI, TDC3ABI"
//result3 = "TDC1ABO , TDC2ABO, TDC3ABO"

Ok, here is my idea of doing the sorting. First, each of the strList sort keyword *ABF. Then, put all the strings with *ABF into result array. Finally do Order sort to have the string array align into TDC1ABF, TDC2ABF, TDC3ABF accordingly. Do the same thing for the other string array inside a loop.

So, my problem is.. how to search *ABF within a string inside a string array?

2
  • Are the arrays guaranteed to have as many elements as there are arrays? Commented May 16, 2013 at 3:17
  • The number of element for each channel are fixed. Just that the arrangement of the element inside an array is sometimes not to be in sequential pattern. And I would also need the array type to do 2D array for multichannel post data processing. So, I decided to use array instead of list to skip the List.ToArray() after I sort the channel name Commented May 16, 2013 at 4:07

3 Answers 3

3
    static void Main()
    {
        var strList1 = new[] { "TDC1ABF", "TDC1ABI", "TDC1ABO" };
        var strList2 = new[] { "TDC2ABF", "TDC2ABI", "TDC2ABO" };
        var strList3 = new[] { "TDC3ABF", "TDC3ABO", "TDC3ABI" };

        var allItems = strList1.Concat(strList2).Concat(strList3);

        var abfItems = allItems.Where(item => item.ToUpper().EndsWith("ABF"))
            .OrderBy(item => item);
        var abiItems = allItems.Where(item => item.ToUpper().EndsWith("ABI"))
            .OrderBy(item => item);
        var aboItems = allItems.Where(item => item.ToUpper().EndsWith("ABO"))
            .OrderBy(item => item);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

If you do something like this then you can compare all the sums and arrange them in order. The lower sums are the ones closer to 1st and the higher are the ones that are farther down.

    static void Main(string[] args)
    {
        string[] strList1 = new string[] { "TDC1ABF", "TDC1ABI", "TDC1ABO" };
        string[] strList2 = new string[] { "TDC2ABF", "TDC2ABI", "TDC2ABO" };
        string[] strList3 = new string[] { "TDC3ABF", "TDC3ABO", "TDC3ABI" };


        arrange(strList1);
        arrange(strList2);
        arrange(strList3);
    }

    public static void arrange(string[] list)
    {
        Console.WriteLine("OUT OF ORDER");
        foreach (string item in list)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine();
        for (int x = 0; x < list.Length - 1; x++)
        {

            char[] temp = list[x].ToCharArray();
            char[] temp1 = list[x + 1].ToCharArray();

            int sum = 0;

            foreach (char letter in temp)
            {
                sum += (int)letter; //This adds the ASCII value of each char 
            }

            int sum2 = 0;

            foreach (char letter in temp1)
            {
                sum2 += (int)letter; //This adds the ASCII value of each char 
            }

            if (sum > sum2)
            {
                string swap1 = list[x];
                list[x] = list[x + 1];
                list[x + 1] = swap1;
            }

        }
        Console.WriteLine("IN ORDER");
        foreach (string item in list)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine();
        Console.ReadLine();
    }

Comments

0

If the arrays are guaranteed to have as many elements as there are arrays then you could sort the individual arrays first, dump the sorted arrays into an nxn array and then transpose the matrix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.