Skip to main content
Id is an int, not a string -_-
Source Link
mjolka
  • 16.3k
  • 2
  • 30
  • 73

Jeroen has already covered some of the problems with Comp_CatID/Comp_CatID_D/Comp_CatName /Comp_CatName _DComp_CatName_D, but there's another one:

try {
    j = Cat_Table1.CatID.CompareTo(Cat_Table2.CatID);
    if (j==1) return -1;
    if (j==-1) return 1;
} catch (Exception) {
    j = 0;
}
return j;

string.CompareTo(string)IComparable<T>.CompareTo(T) does not guarantee that it will only ever return -1, 0, or 1. So if j == 2, this method will return 2.

You also don't want to do this:

return -Cat_Table1.CatName.CompareTo(Cat_Table2.CatName);

(try running int x = int.MinValue; Console.WriteLine(x == -x);)

I would suggest that you remove the classes and instead use List<T>.Sort(Comparison<T>) like so:

// sort by IDCatID ascending
this.InnerList.Sort((x, y) => string.Compare(x.Id, CatID.CompareTo(y.IdCatID));

// sort by IDCatID descending
this.InnerList.Sort((x, y) => string.Compare(y.Id, CatID.CompareTo(x.IdCatID));

// sort by NameCatName ascending
this.InnerList.Sort((x, y) => string.Compare(x.NameCatName, y.NameCatName));

etc.

Please see the list of Stringstring.Compare overloads to see which one is right for your use-case.

Jeroen has already covered some of the problems with Comp_CatID/Comp_CatID_D/Comp_CatName /Comp_CatName _D, but there's another one:

try {
    j = Cat_Table1.CatID.CompareTo(Cat_Table2.CatID);
    if (j==1) return -1;
    if (j==-1) return 1;
} catch (Exception) {
    j = 0;
}
return j;

string.CompareTo(string) does not guarantee that it will only ever return -1, 0, or 1. So if j == 2, this method will return 2.

You also don't want to do this:

return -Cat_Table1.CatName.CompareTo(Cat_Table2.CatName);

(try running int x = int.MinValue; Console.WriteLine(x == -x);)

I would suggest that you remove the classes and instead use List<T>.Sort(Comparison<T>) like so:

// sort by ID ascending
this.InnerList.Sort((x, y) => string.Compare(x.Id, y.Id));

// sort by ID descending
this.InnerList.Sort((x, y) => string.Compare(y.Id, x.Id));

// sort by Name ascending
this.InnerList.Sort((x, y) => string.Compare(x.Name, y.Name));

etc.

Please see the list of String.Compare overloads to see which one is right for your use-case.

Jeroen has already covered some of the problems with Comp_CatID/Comp_CatID_D/Comp_CatName /Comp_CatName_D, but there's another one:

try {
    j = Cat_Table1.CatID.CompareTo(Cat_Table2.CatID);
    if (j==1) return -1;
    if (j==-1) return 1;
} catch (Exception) {
    j = 0;
}
return j;

IComparable<T>.CompareTo(T) does not guarantee that it will only ever return -1, 0, or 1. So if j == 2, this method will return 2.

You also don't want to do this:

return -Cat_Table1.CatName.CompareTo(Cat_Table2.CatName);

(try running int x = int.MinValue; Console.WriteLine(x == -x);)

I would suggest that you remove the classes and instead use List<T>.Sort(Comparison<T>) like so:

// sort by CatID ascending
this.InnerList.Sort((x, y) => x.CatID.CompareTo(y.CatID));

// sort by CatID descending
this.InnerList.Sort((x, y) => y.CatID.CompareTo(x.CatID));

// sort by CatName ascending
this.InnerList.Sort((x, y) => string.Compare(x.CatName, y.CatName));

etc.

Please see the list of string.Compare overloads to see which one is right for your use-case.

Source Link
mjolka
  • 16.3k
  • 2
  • 30
  • 73

Jeroen has already covered some of the problems with Comp_CatID/Comp_CatID_D/Comp_CatName /Comp_CatName _D, but there's another one:

try {
    j = Cat_Table1.CatID.CompareTo(Cat_Table2.CatID);
    if (j==1) return -1;
    if (j==-1) return 1;
} catch (Exception) {
    j = 0;
}
return j;

string.CompareTo(string) does not guarantee that it will only ever return -1, 0, or 1. So if j == 2, this method will return 2.

You also don't want to do this:

return -Cat_Table1.CatName.CompareTo(Cat_Table2.CatName);

(try running int x = int.MinValue; Console.WriteLine(x == -x);)

I would suggest that you remove the classes and instead use List<T>.Sort(Comparison<T>) like so:

// sort by ID ascending
this.InnerList.Sort((x, y) => string.Compare(x.Id, y.Id));

// sort by ID descending
this.InnerList.Sort((x, y) => string.Compare(y.Id, x.Id));

// sort by Name ascending
this.InnerList.Sort((x, y) => string.Compare(x.Name, y.Name));

etc.

Please see the list of String.Compare overloads to see which one is right for your use-case.