2

I've got a two dimensional array containing the amount of sold articles with 6 different salesmen. I'm trying to sort the array to find the highest amount of sold articles of them all.

My problem is being able to sort the two dimensional array and to read its content.

I've tried

        int[,] sortedByFirstElement = nrArticles.OrderBy(x => x[0]);

Which gives me an error message saying: "'int[,] does not contain a definition for 'OrderBy' and no accessible extension method... etc"

I've tried which gives me an error message claiming the input is not in int.

        Array.Sort(nrArticles);
        foreach (int value in nrArticles)
        {
            Console.Write(value);

        }

The following code is what i've got so far excluding the sorting attempts.

        string[,] salesMan = new string[6, 3];
        int[,] nrArticles = new int[6, 0];

        string line;
        int nr;

        for (int i = 0; i < 5; i++)
        {

            Console.Write("Enter the name of salesman: ");
            line = Console.ReadLine();

            salesMan[i, 0] = line;

            Console.Write("Enter the social number: ");
            line = Console.ReadLine();
            salesMan[i, 1] = line;


            Console.Write("Enter the district: ");
            line = Console.ReadLine();
            salesMan[i, 2] = line;

            //This is where i convert the entered value into an int taken 
            by the array
            Console.Write("Enter the amount of sold articles: ");
            line = Console.ReadLine();
            nr = Int32.Parse(line);
            nrArticles[i, 0] = nr;




        }

        Console.Write("Namn \t\tPersnr \t\t\tDistrikt \t\tAntal");
        for (int j = 0; j < 5; j++)
        {

           Console.Write("\n"+salesMan[j, 0] +"\t\t");
           Console.Write(salesMan[j, 1] + "\t\t\t");
           Console.Write(salesMan[j, 2] + "\t\t\t");
           Console.Write(nrArticles[j, 0]);
        }

         //Here's where i'd put my code of sorting my integer array 
         nrArticles (If i had one). 

    }

}

The expected result of it would to see an example like: 231, 183, 130, 122, 40, 10. Maybe as a separate array, or even string?

Any help would be greatly appreciated.

1
  • This is a great question, but I am curious as to why you're not using Dictionary<int, List<int>> instead? You can use LINQ with that. Commented Jan 14, 2019 at 17:33

2 Answers 2

2

The short answer, I think, is that the 2D array doesn't have a built-in OrderBy method, so you would need to handle that on your own. I think you were on the right track with the Array.Sort approach, but that's still limited to sorting one-dimensional arrays.

Others have alluded to this, but I think you're going to have a good deal of trouble with the 2D arrays of integers. Unless there's a specific reason to be doing this as you are, I think you would be better served by creating an class for your salesmen which encapsulate the full set of properties related to a single salesman as an entity. (E.g. Name, Districts, Articles Sold, etc.).

Once you have a well-defined type definition, LINQ (for example) can give you a good deal of flexibility in how you wish to organize a collection of those objects.

Here's some very simplistic sample code:

using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        public class Salesman
        {
            public string Name { get; set; }
            public int District { get; set; }
            public int ArticlesSold { get; set; }
        }

        static void Main(string[] args)
        {

            var salesmen = new Salesman[] {
                 // Fill collection by some means...
            };

            // Then for example:
            // sort by Ascending sales count
            var sorted = salesmen.OrderBy(x => x.ArticlesSold);

            // or descending
            sorted = salesmen.OrderByDescending(x => x.ArticlesSold);

            // or by something more complex
            sorted = salesmen.OrderBy(x => x.ArticlesSold).ThenBy(x => x.District);
        }

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

1 Comment

I decided to do as you recommended with creating a separate class containing the salesmen. When a salesman was added i put them in a List which was easily sorted with the OrderBy. It was 10x easier to work with that the array. Thanks alot!
1

Use an int[][] instead; this way, you can use Linq on the structure.

int[][] arr = new int[6][];
for(int i =0; i<6; i++)
{
    arr[i] = new int[1];
}
arr[0][0] = 100;
arr[1][0] = 50;
arr[2][0] = 60;
arr[3][0] = 200;
arr[4][0] = 10;
arr[5][0] = 220;

int[][] sortedArr = arr.OrderBy(x => x[0]).ToArray();
for(int i =0; i<6; i++)
{
    Console.WriteLine(sortedArr[i][0]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.