Why am I getting a System.ArgumentException when I call Sort (IComparer) on a list?

I sort the list with my own IComparer, and it works fine when running the application (XNA game) for more than an hour. But then, unexpectedly, I sometimes get the following error when calling the sort method with my custom Comparer:

An unhandled exception of type 'System.ArgumentException' occured in mscorlib.dll
Additional Information: ArgumentException

This is the line where the exception is thrown:

List<Continent> markets = new List<Continent>();
// filling the markets list ...
markets.Sort(new MarketCostCoverComparer(this)); 

and this is my class implementing the IComparer interface:

class MarketCostCoverComparer : IComparer<Continent> { 

    private Player player; 

    public MarketCostCoverComparer(Player player) { 
        this.player=player; 
    } 

    public int Compare(Continent c1, Continent c2) { 
        if(player.GetCostCovering(c1)<player.GetCostCovering(c2)) { 
            return +1; 
        } else if(player.GetCostCovering(c1)==player.GetCostCovering(c2)) { 
            return 0; 
        } else { 
            return -1; 
        } 
    } 

} 

Here are some methods related to comparator ...:

public float GetCostCovering(Continent continent) {
        // cover<1 => bad | cover>1 => good
        if(GetOilfieldTheoreticOutput(continent.Type, true)<continent.Economy.CurrentDemand) {
            return ((float)((GetOilfieldTheoreticOutput(continent.Type, true)*continent.Economy.CurrentPrice)))/(float)GetOilfieldCosts(continent.Type, true);
        } else {
            return ((float)((continent.Economy.CurrentDemand*continent.Economy.CurrentPrice)))/(float)GetOilfieldCosts(continent.Type, true);
        }
    }

public int GetOilfieldTheoreticOutput(ContinentType continent, bool drilled) {
        int total = 0;
        foreach(Oilfield oilfield in worldmap.Continents[(int)continent].Oilfields) {
            if(oilfield.Owner==this && oilfield.Drilled==drilled) {
                total+=oilfield.TheoreticOutput;
            }
        }
        return total;
    }

public int GetOilfieldCosts(ContinentType continent, bool drilled) {
        int total = 0;
        foreach(Oilfield oilfield in worldmap.Continents[(int)continent].Oilfields) {
            if(oilfield.Owner==this && oilfield.Drilled==drilled) {
                total+=oilfield.Costs;
            }
        }
        return total;
    }

Here is a screenshot of the exception:

An unhandled exception of type 'System.ArgumentException' occured in mscorlib.dll

Here's a closer look at Locals / Stack-Trace (this is an old screenshot, but I will try to reproduce this exception in the next hours so I can extend the trace):

enter image description here

+5
source share
3 answers

IComparer. , sort .

, .

:

continent.Economy.CurrentDemand continent.Economy.CurrentPrice ?

:

IComparer null. docs:

IComparable. null .

, , . , , decimal float.

+3

, ( XNA):

-, , 0 . , GetCostCovering (c1) GetCostCovering (c2) , ?

if (c1 == c2) return 0; Compare , .

!

0

, IComparer.Compare , . , , , . , ...

-1

All Articles