Amer Amer - 23 days ago 7
C# Question

Organizing a list of of strings based on a list of integers c#

I have an array of integers organized from greatest to least, however, I want the numerical values to be associated with a name string entered by the user and ordered from greatest to least based on that numerical value.
The array is organized like this:

int[] array = new int[] { diceRoll1, diceRoll2, diceRoll3, diceRoll4, diceRoll5, diceRoll6, diceRoll7, diceRoll8 };
new Comparison<int>(
(i1, i2) => i2.CompareTo(i1)

The list of strings is entered by the user, diceRoll1 goes with name1, diceRoll2 goes with name2 etc.

string name1 = nameBox1.Text;
string name2 = nameBox2.Text;
string name3 = nameBox3.Text;
string name4 = nameBox4.Text;
string name5 = nameBox5.Text;
string name6 = nameBox6.Text;
string name7 = nameBox7.Text;
string name8 = nameBox8.Text;


Since you are taking the strings from textboxes, you can build the textbox name dynamically by using the numbers. I assume that the int array contains the indexes of the strings from 1 to 8 in the desired order.

string[] textArray = intArray
    .Select(i => Controls("nameBox" & array[i]).Text)

If you simply want the strings in the order nameBox8 down to nameBox1, you can do it like this

string[] textArray = Enumerable.Range(0, 8)
    .Select(i => Controls("nameBox" & (8 - i)).Text)