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))));
8
  • Can you start by creating code that counts the number of true values in an array? Commented Sep 10, 2018 at 15:09
  • I have done that, are you asking for me to add it to the question? Commented Sep 10, 2018 at 15:09
  • Please do. Questions with at least some attempt are much more likely to receive good answers. Commented Sep 10, 2018 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. Commented Sep 10, 2018 at 15:10
  • 1
    Yep, in the spirit of SO's rule "show what you've done so far" Commented Sep 10, 2018 at 15:10

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

4 Comments

I prefer boolList.OrderByDescending(x => x.Cast<bool>().Count(i => i == true)).ToList();
@MarcoSalerno - can do but it is basically the same :) just like writing if(someBoolean) or if(someBoolean == true)
@MarcoSalerno - well that is a matter of opinion obviously :)
Another option would be to use that lambda with List<T>.Sort.
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();
    }
}

7 Comments

I cant see how this does anything different to Gilad's answer, except that it's more wordy. Wordy does not necessarily imply readable
From my point of view it's more readable and you can actually order by true or false if you need ^^
Maybe, though you can also do that in Gilad's answer either by OrderBy( or Count(x=> !x) ?
Gilad answer doesn't cover that need, what's the point of your comments?
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
|

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.