0

Hoping I can explain this clearly ... I have a collection of variables:

static string sRunnerSetName, sFH, sR1, sH2, sR2, sH3, sR3, sH4, sR4, sH5, sR5 = "";
static int iRunnerSetName, iFH, iR1, iH2, iR2, iH3, iR3, iH4, iR4, iH5, iR5 = 0;

Each of the int variables hold a unique value, which provide the order that the corresponding string variables need to be combined and put into a concatenated string. So iFH holds the sorting/order-number position for where the string sFH will be positioned in the concatenated string.

I'm just stuck with how to use the values in each int to create the order of strings?

As an example -

 iFH = 2; i1R = 0; i2R = 1;
 sFH = "z"; s1R = "x"; s2R = "y";

Looking to use the values in the integer variables to create the order/position of each string so that the concatenated result of the above would be "xyz".

5
  • 1
    Maybe provide an example because I've read your question twice now and I still have no idea what you're asking. Commented Sep 9, 2021 at 7:23
  • Sorry, I've been trying to figure out how to ask correctly. I'll put a example in. Commented Sep 9, 2021 at 7:26
  • Is it necessary to store your values in dedicated variable? I would store them in an array, than it would be easyer to iterate trough them do whatever you want with it. It can be done this way too but you dont want to go for the "reflection way" I think Commented Sep 9, 2021 at 7:27
  • Usually that's what I would do, but the integer values are being brought in from an external file that is a collection of columns (tab separated list file), the integers are the numbers of each column. Commented Sep 9, 2021 at 7:32
  • Why does that mean you cannot store them in an array? Just make the index the column number Commented Sep 9, 2021 at 9:23

5 Answers 5

3

Create a class holding a string and an int:

class Item
{
    public string Description {get;set;}
    public int SortOrder {get;set;}
}

Create a list (or another collection, which fits better to your needs) of these items:

List<Item> list = new List<Item>
{
   new Item { Description = "Test", SortOrder = 4 },
   new Item { Description = "Test2", SortOrder = 3 },
   new Item { Description = "sadf", SortOrder = 1 },
   new Item { Description = "Example", SortOrder = 2 },
   new Item { Description = "something", SortOrder = 5 }
};

You can use LINQ to sort your list:

list = list.OrderBy(x => x.SortOrder).ToList();

You can then output it on console:

Console.WriteLine(string.Join("\n", list.Select(x => x.Description)));

Try it online

Sign up to request clarification or add additional context in comments.

2 Comments

minor note: if we don't care about preserving the original list order for prosperity: list.Sort(static (x, y) => x.SortOrder.CompareTo(y.SortOrder)); will be more efficient (in-place sort, no additional allocations)
I just added one more thing with this... I put it into a new variable to be able to remove the line feed so I can output it all into one line so it's separated by tabs instead of line feeds. string strOutputLine = string.Join("\t", list.Select(x => x.Description)); strOutputLine = strOutputLine.Replace("\r", ""); I'm sure there's a better way to do it but very new to C# :)
1

You could use arrays here; copy the data into the arrays, then sort them using the index one as the master:

        string a = "a", b = "b", c = "c", d = "d";
        int ia = 3, ib = 2, ic = 0, id = 1;

        
        string[] sarr = null;
        int[] iarr = null;
        try
        {
            // put the data into vectors; we can't talk about variables
            // abstractly, but we *can* talk about vectors by position
            sarr = ArrayPool<string>.Shared.Rent(4);
            iarr = ArrayPool<int>.Shared.Rent(4);
            sarr[0] = a;
            sarr[1] = b;
            sarr[2] = c;
            sarr[3] = d;
            iarr[0] = ia;
            iarr[1] = ib;
            iarr[2] = ic;
            iarr[3] = id;

            var sb = new StringBuilder();
            Array.Sort(iarr, sarr, 0, 4);
            for (int i = 0; i < 4; i++)
            {
                sb.Append(sarr[i]);
            }
            sb.AppendLine();
            Console.WriteLine(sb.ToString());
        }
        finally
        {
            if (sarr is not null) ArrayPool<string>.Shared.Return(sarr);
            if (iarr is not null) ArrayPool<int>.Shared.Return(iarr);
        }

Not super efficient, but it would work. However, it is probably better to re-frame the problem; from your example:

iFH = 2; i1R = 0; i2R = 1;
sFH = "z"; s1R = "x"; s2R = "y";

If we instead say:

string[] sarr = { "z", "x", "y"};

and now talk in terms of what tokens you want, by position:

int[] iarr = { 1, 2, 0 };

now you can just use:

foreach (int i in iarr) {
    sb.Append(sarr[i]);
}

Comments

1

One possible solution: use a SortedDictionary<int, string> like this:

        int iFH = 2, i1R = 0, i2R = 1;
        string sFH = "z", s1R = "x", s2R = "y";

        var map = new SortedDictionary<int, string>();
        map[iFH] = sFH;
        map[i1R] = s1R;
        map[i2R] = s2R;

        var result = string.Join("", map.Values);

Comments

1

If I understand correctly, you mean to use the int values as order number in an array of strings. Since the int values are of type int, you could directly use them as values. For Example, assuming you have an array of strings called stringArray,

stringArray[iFH] = sFH;

Doing this for all the strings you can make an ordered array. To concatenate them all, you can iterate over the array and add them to a seperate string in the following way:

String finalString = "";
for(int i = 0; i < stringArray.Length; i++){

    finalString = finalString + stringArray[i];

}

console.WriteLine(finalString);

Comments

0

Splitting the problem into two

  1. Converting an arbitrarily long random list of variables into an array
  2. Using the array to create a concatenated string

For part 1, create a function that takes params

public static string[] StringList(params object[] stringValues) => stringValues.Cast<string>().ToArray();

public static int[] PositionList(params object[] intValues) => intValues.Cast<int>().ToArray();

For part 2

public static string Join(string[] text, int[] positions)
{
    string[] final = new string[text.Length];
    for (int i = 0; i < text.Length; i++)
    {
        final[positions[i]] = text[i];
    }
    return string.Join("", final);
}

Then run like this:

public static string RunExample()
{
    string sFH = "z"; string s1R = "x"; string s2R = "y";
    int iFH = 2; int i1R = 0; int i2R = 1;
    return Join(StringList(sFH, s1R, s2R), PositionList(iFH, i1R, i2R));
}

Entire example:

public static class Joiner
{
    public static string[] StringList(params object[] stringValues) => stringValues.Cast<string>().ToArray();

    public static int[] PositionList(params object[] intValues) => intValues.Cast<int>().ToArray();

    public static string Join(string[] text, int[] positions)
    {
        string[] final = new string[text.Length];
        for (int i = 0; i < text.Length; i++)
        {
            final[positions[i]] = text[i];
        }
        return string.Join("", final);
    }

    public static string RunExample()
    {
        string sFH = "z"; string s1R = "x"; string s2R = "y";
        int iFH = 2; int i1R = 0; int i2R = 1;
        return Join(StringList(sFH, s1R, s2R), PositionList(iFH, i1R, i2R));
    }
}

You can add your own code for exception handling (mismatch in array sizes etc).

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.