I'm having a problem with the delegate not delegating... I have an object called Tweet that has a string text and an int score. I want to sort the array of tweet objects(twtArray) in order of the score. This is the code I have:
Array.Sort(twtArray, delegate(Tweet twt1, Tweet twt2)
{
return twt1.score.CompareTo(twt2.score); //(twt1.score - twt2.score)
});
and it throws:
System.NullReferenceException: Object reference not set to an instance of an object. at System.Array.FunctorComparer`1.Compare(T x, T y)
Whilst debugging, I noticed that the first comparison works but in the second comparison, twt2
is null. And it can't be null because I definitely have 8 elements in the array.
I've tried reversing twt1
and twt2
as well but makes no difference.
I also tried making my own comparison method in the Tweet class but again, same thing.
Any help would be appreciated!
Also, I dont think this is a duplicate of this question: List.Sort in C#: comparer being called with null object because i tried all the possible solutions from this but it's not working. i've also searched a lot on google :(