1

I'm trying to sort a list containing bool 2D arrays like below.

List<bool[,]>boolList;

bool[,] bool2DArray = {
    {true,true,true,true},
    {true,true,true,true},
    {true,true,true,true}
};

I've been trying to sort the list they're in by the amount of true counts within each 2D Array.

After some research and looking over Stack Overflow I haven't been able to find a solution for this specific issue; many of the solutions I was able to find wouldn't work for me in this situation where I'm not comparing them directly, but rather the result of a calculation on them which led me to trying with a Lambada type solution which also failed but I think that might have been due to me not understanding how to implement it correctly.

Edit

Simple function I made for getting the count

int GetCount(bool[,]bool2DArray) {
    int count = 0;
    int rows = bool2DArray.GetUpperBound(0);
    int columns = bool2DArray.GetUpperBound(1);


    for (int x = 0; x <= rows; x++) {
        for (int i = 0; i <= columns; i++) {
            bool isTrue = bool2DArray[x, i];

            if (isTrue) {
                count++;
            }
        }
    }
    return count;
}

And this is the Lambada type solution I think is in the right direction but isn't valid.

List<bool[,]> sortedList = boolList.Sort((a,b) => (GetCount(a).CompareTo(GetCount(b))));
halfer
  • 19,824
  • 17
  • 99
  • 186
akaBase
  • 2,178
  • 1
  • 18
  • 31
  • Can you start by creating code that counts the number of `true` values in an array? – itsme86 Sep 10 '18 at 15:09
  • I have done that, are you asking for me to add it to the question? – akaBase Sep 10 '18 at 15:09
  • Please do. Questions with at least some attempt are much more likely to receive good answers. – itsme86 Sep 10 '18 at 15:10
  • Pick a sort and adapt that code to your data structure. You can write a bubble sort or find p-code on Wikipedia and all you have to add is the logic to compare two objects and the logic to swap two objects. – nicomp Sep 10 '18 at 15:10
  • 1
    Yep, in the spirit of SO's rule "show what you've done so far" – Caius Jard Sep 10 '18 at 15:10
  • You just need to group data and order it – Mrinal Kamboj Sep 10 '18 at 15:12
  • You said you have a list of 2D arrays, but I don't see it? I see an empty list and a single 2D array.. – Caius Jard Sep 10 '18 at 15:17
  • As an aside,is it you who creates these 2D arrays (`bool[,]`).. if so, can you make them as `bool[][]` instead? Rectangular arrays aren't really that useful, compared to jaggies – Caius Jard Sep 10 '18 at 15:33

2 Answers2

2

You'd first want to see how to easily work with the 2d array and count the number of trues in it. To do so for a single item, you could do something similar to what is found in this question: Fast way to convert a two dimensional array to a List ( one dimensional )

bool2DArray.Cast<bool>().Count(i => i)

Then wrapping that with OrderDescendingBy you get the desired result:

var collection = new List<bool[,]> { bool2DArray, ... };
var result = collection.OrderByDescending(item => item.Cast<bool>().Count(i => i));
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
0

I prefer this approach which I think is more readable and cover more usecases:

class Program
{
    static void Main(string[] args)
    {
        List<bool[,]> boolList = new List<bool[,]>() {
        new bool[,] {
            {true,true,true,true},
            {true,true,true,true},
            {true,true,true,true}
        },
        new bool[,] {
            {false,true,true,true},
            {false,true,true,true},
            {false,true,true,true}
        }
        };

        boolList = OrderBoolArraysByBoolean(boolList, true);
    }

    private static List<bool[,]> OrderBoolArraysByBoolean(List<bool[,]> listOfArrays, bool value)
    {
        return listOfArrays.OrderByDescending(x => x.Cast<bool>().Count(i => i == value)).ToList();
    }
}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • 1
    I cant see how this does anything different to Gilad's answer, except that it's more wordy. Wordy does not necessarily imply readable – Caius Jard Sep 10 '18 at 15:31
  • From my point of view it's more readable and you can actually order by true or false if you need ^^ – Marco Salerno Sep 10 '18 at 15:32
  • Maybe, though you can also do that in Gilad's answer either by `OrderBy(` or `Count(x=> !x)` ? – Caius Jard Sep 10 '18 at 15:35
  • Gilad answer doesn't cover that need, what's the point of your comments? – Marco Salerno Sep 10 '18 at 15:36
  • Was that need asked for? (== was it even a need?). Purpose of comemnts is for OP and future visitors to let them know that this answer doesn't necessarily answer the question, or add anything that the accepted answer doesn't already do – Caius Jard Sep 10 '18 at 15:36
  • Maybe you don't get the point of a site like this, having answers which cover more usecases isn't bad or wrong – Marco Salerno Sep 10 '18 at 15:38
  • Oh well, by that logic, SO would only need one question (with several million answers), if you're going to answer all the questions a person didn't ask but might have :) – Caius Jard Sep 10 '18 at 15:39