Reputation: 161
I have a GinList class that I want to compare 2 string lists of the class
public class GinList
{
public string GinName;
public string Country;
public string Abv;
public string Price;
public string Votes;
public string Stars;
public GinList(string ginName, string country, string abv, string price, string votes, string stars )
{
GinName = ginName;
Country = country;
Abv = abv;
Price = price;
Votes = votes;
Stars = stars;
}
}
I then populate the the gins List ..
public class TestScript : MonoBehaviour
{
public string ginString;
private List< GinList > sortedResults = new List< GinList >();
private List< GinList > gins = new List< GinList >();
private string[] splitGinString;
void Start ()
{
splitGinString = ginString.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
// build the new GinList()
for (var i = 0; i < splitGinString.Length-1; i++)
{
gins.Add(new GinList(splitGinString[i], splitGinString[i+1], splitGinString[i+2],
splitGinString[i+3], splitGinString[i+4], splitGinString[i+5]));
i+=6;
}
}
}
Then a Debug shows ..
0 is .. 5th Fire - Spain - 42.00% - £3.80 - 0 - 0
1 is .. Biercee - Belgium - 44.00% - £4.30 - 0 - 0
2 is .. Blackwood - Scotland - 40.00% - £3.60 - 0 - 0
3 is .. Blind Tiger - Piper Cubeba - Belgium - 47.00% - £4.00 - 0 - 0
4 is .. Bloom - England - 42.00% - £3.50 - 0 - 0
ect ..
Then I do another List, Gins with some missing, I then try both ..
sortedResults = gins.Except( Gins ).ToList();
sortedResults = Gins.Except( gins ).ToList();
Then a ..
foreach(GinList gin in sortedResults)
{
Debug.Log("Sorted Gin number " + nextPos + " is .. " + gin.GinName + " - " + gin.Country + " - "
+ gin.Abv + " - " + gin.Price + " - " + gin.Votes + " - " + gin.Stars);
nextPos++;
}
But the Debug shows the full List, what am I doing wrong? Thanks.
Upvotes: 0
Views: 74
Reputation: 62213
You should read the documentation for Except:
The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.
In short you need to implement the equality methods on your type.
On a side note you should not make fields public, instead use properties to encapsulate the field. You can use auto-properties to do most of the work for you so you do not need a field + property. You can also specify that the setter is private
if it should never be set outside the class. You can omit set
all together if it is not set outside the constructor and you are using c# 6.0 or later.
Also as mentioned you should follow common naming conventions. For properties use pascal case.
Example: field _ginName
would become property GinName
.
public string GinName { get; set; }
Upvotes: 4