I've used IComparer before but I seem to be getting an odd issue where I'm not getting the second argument to my Comparer function.

internal class FreePtrCompare : IComparer<File>
{
      int IComparer<File>.Compare(File x, File y)
      {
            int a = x.fsPos; //Free space pointer of file 1
            int b = y.fsPos; //Free space pointer of file 2
            return a.CompareTo(b);
      }
}
File [] takenSpace = HardDrive; //HardDrive is also a Array of Files
Array.Sort(takenSpace, new FreePtrCompare());

Basically as soon as it gets to the "Array.Sort" line it crashes where "int b" is declared in the "FreePtrCompare" class saying that File y is null object/pointer reference. which it is, but I'm not entirely sure why that isn't being passed through correctly.

Has anyone else run into this issue before?

That code won't compile and i'm not aware of where you are getting the property fsPos from. Could you post a version of the code I can compile?

Basically as soon as it gets to the "Array.Sort" line it crashes where "int b" is declared in the "FreePtrCompare" class saying that File y is null object/pointer reference. which it is, but I'm not entirely sure why that isn't being passed through correctly.

If File y is null, what do you expect to happen? What is y.fsPos supposed to do when y is null? (Hint: it throws an exception.)

Apologies for the snark.

You should modify your Compare method to explicitly handle the case where parameters are null.

Edit: or you could remove nulls from the array that you're sorting, if that's the right thing to do.

That's the thing though. It crashes getting even the second file from the list. by default there are 5 elements in HardDrive, it gets the very first element when Compare is called, but it doesn't get the second elements for File y. It should just be traversing through the array and comparing them but for some reason it doesn't seem to be getting the next element.

The "second" file from the list? Are you saying you're expecting Sort to look at the elements of the list in some particular order?

Here's my project. To run it you'll want to type in a name at the command prompt, then type "free space full" as that's the command that will start the sorting function.

I'm not so sure I expected the Array.Sort to go through any particular order, but I expected it to only compare spots in which a file actually existed. If I declare the array to be 64 spaces large, then only fill 5 of them, and tell it to do an Array.Sort does it sort even blank spaces?

If that's the case I guess I could just as easily push each one to a List<File> and then call Array.Sort on that.

I'm not so sure I expected the Array.Sort to go through any particular order, but I expected it to only compare spots in which a file actually existed. If I declare the array to be 64 spaces large, then only fill 5 of them, and tell it to do an Array.Sort does it sort even blank spaces?

There's no such thing as a 'blank space.' An array with 64 elements will contain 64 elements. Some of them will be null, because that's what they were initialized with -- but that's no different than if you had assigned null to any particular element. Array.Sort will try to sort the entire array.

If that's the case I guess I could just as easily push each one to a List<File> and then call Array.Sort on that.

Yes. You shouldn't preallocate arrays and then put several values in them, you should just use List<>.

commented: This pains me to +rep you, but it is deserved. +5

Yep, that sure did it! :)
The only reason I was using an Array of pre-defined size was because the HardDrive acting as a real hard drive needed to have a pre-allocated size. I just figured that the compare method wouldn't try to compare null values in an array but it makes sense that it would.

Thanks a lot for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.