-3

I'm making a program to automate rolling initiative. I have most of it done, but I can't sort the outputs because it's a jagged array. I need the second column of each array in my jagged array to be sorted from highest to lowest.

using System;
using System.Linq;

namespace Auto_Initiative
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] encounter =
            {
                "Wizard", "18", "-2",
                "Bard", "9", "3",
                "Goblin 1", "16", "1",
                "Goblin 2", "14", "1"
            };

            int[][] numbers = new int[encounter.Length / 3][];
            int loop = 0;
            for(int i = 0; i > encounter.Length; i += 3)
            {
                // Name number, real roll
                numbers[loop] = new int[2] {i, Int32.Parse(encounter[i + 1]) + Int32.Parse(encounter[i + 2])};
            }
            Console.ReadKey();
        }
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 12
    Why are you using jagged array here? Why use array at all and have numbers as strings. Create a class to hold these three fields and use a list/array of class instances. – Sergey Berezovskiy Jan 24 '21 at 00:31
  • This is not a jagged array this is just `string[]` and that threw me off while putting an answer. A jagged array in this situation would upon expectation be `string[][]` or perhaps `string[,]`. If you insisted on making it a jagged array then you should have each line as it's own `string[]`. – Omar Abdel Bari Jan 24 '21 at 02:54
  • Does this answer your question? [Sorting a jagged array by second column](https://stackoverflow.com/questions/19526086/sorting-a-jagged-array-by-second-column) and [Sorting a jagged array in C# by multiple columns](https://stackoverflow.com/questions/40362049/sorting-a-jagged-array-in-c-sharp-by-multiple-columns) and [How do I sort jagged array by row in C#](https://stackoverflow.com/questions/8162348/how-do-i-sort-jagged-array-by-row-in-c) and [C# Sorting a jagged array of objects](https://stackoverflow.com/questions/43306520/c-sharp-sorting-a-jagged-array-of-objects) –  Jan 24 '21 at 05:45

2 Answers2

2

One part of designing your software is choosing the right data structure for how you are planning to use it. Sometimes redundant data is required but we don't know what you are requirements are to make that decision. So as was mentioned by Sergey you should consider creating a custom class which I have shown an example of below. Also note that a string[] is not really a jagged array. By definition a jagged array has nested arrays of variable size. The data structure depicted above could be put in a regular string[][] and would not be jagged.

Object Oriented in Action

What you are looking for is stored in unitsSortedBySecondColumn.

class so65865986
{
    static void Main(string[] args)
    {
        Encounter encounter = new Encounter
        {
            Units = new List<EncounterUnit> { 
                new EncounterUnit{
                    Name = "Wizard",
                    Column1 = 18,
                    Column2 = -2,
                },
                new EncounterUnit{
                    Name = "Bard",
                    Column1 = 9,
                    Column2 = 3,
                },
                new EncounterUnit{
                    Name = "Goblin 1",
                    Column1 = 16,
                    Column2 = 1,
                },
                new EncounterUnit{
                    Name = "Goblin 2",
                    Column1 = 14,
                    Column2 = 1,
                },

            },
        };

        var unitsSortedBySecondColumn = encounter.Units
            .OrderBy(u => u.Column1)
            .Select(u => new int[] { u.Column1, u.Column2 })
            .ToArray();

    }


}

class EncounterUnit
{
    public string Name;
    public int Column1; //Change name to whatever it means
    public int Column2; //Change name to whatever it means
}

class Encounter
{
    public List<EncounterUnit> Units;
}

Nested (but not Jagged) Array

class so65865986_nested_array
{
    static void Main(string[] args)
    {
        string[][] encounter =
        {
            new string[] {"Wizard", "18", "-2" },
            new string[] {"Bard", "9", "3" },
            new string[] {"Goblin 1", "16", "1" },
            new string[] {"Goblin 2", "14", "1" },
        };

        int[][] numbers = encounter
            .Select(u => new int[] { int.Parse(u[1]), int.Parse(u[2]) })
            .OrderBy(u => u[0])
            .ToArray();

        Console.ReadKey();
    }
}

Other Notes

Also, another note. You don't need to use Int32 because it is recommended you use the aliases provided which in this case is int.

Omar Abdel Bari
  • 934
  • 9
  • 17
0

use this code:

string[] encounter =
{
   "Wizard", "18", "-2",
   "Bard", "9", "3",
   "Goblin 1", "16", "1",
   "Goblin 2", "14", "1"
};

int[,] numbers = new int[encounter.Length / 3, 3];
for (int i = 1; i < encounter.Length / 4; i++)
{
   for (int j = 0; j < encounter.Length / 3; j += 1)
   {
      numbers[j, i] = Convert.ToInt32(encounter[i + (j * 3)]);
      Console.Write(numbers[j, i] + "    ");
   }
   Console.WriteLine(Environment.NewLine);
}
Console.ReadLine();
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17