Skip to main content
Fixed the `GetHashCode` method.
Source Link
user33306
user33306
class Motorcycle:IComparable<Motorcycle>
{
    string _name = "";
    int _speed = 0;
    int _price = 0;
    int _weight = 0;
    static int _compareOrder = 0;
    public string Name
    {
        get { return _name; }
        private set{_name = value;}
    }
    public int Speed
    {
        get { return _speed; }
        private set { _speed = value; }
    }
    public int Price
    {
        get { return _price; }
        private set { _price = value; }
    }
    public int Weight
    {
        get { return _weight; }
        private set { _weight = value; }
    }
    public static int CompareOrder
    {
        private get { return _compareOrder; }
        set
        {
            if(value >= 0 && value < sortOrders.Count)
            {
                _compareOrder = value;
            }
            else
            {
                throw new ArgumentOutOfRangeException($"Value must be between 0 and {sortOrders.Count - 1}, inclusive. Value entered is {value}.");
            }
        }
    }
    enum Properties
    {
        Name,
        Price,
        Speed,
        Weight
    }
    static readonly List<List<Properties>> sortOrders = new List<List<Properties>>()
    {
        new List<Properties>(){Properties.Price, Properties.Speed, Properties.Weight, Properties.Name },
        new List<Properties>(){Properties.Speed, Properties.Weight, Properties.Price, Properties.Name},
    };

    public Motorcycle(){}
    public Motorcycle(string name, int price, int speed, int weight)
    {
        _name = name;
        _speed = speed;
        _price = price;
        _weight = weight;
    }
    public Motorcycle(params String[] args)
    {
        _name = args[0];
        if(!int.TryParse(args[1],out _price))
        {
            throw new ArgumentException($"parameter index 1 will not parse to int.  Value is {args[1]}.");
        }
        if (!int.TryParse(args[2], out _speed))
        {
            throw new ArgumentException($"parameter index 2 will not parse to int.  Value is {args[2]}.");
        }
        if (!int.TryParse(args[3], out _weight))
        {
            throw new ArgumentException($"parameter index 3 will not parse to int.  Value is {args[3]}.");
        }
    }
    public override string ToString()
    {
        return _name;
    }
    public override int GetHashCode() => base$"{_name}{_price}{_speed}{_weight}".GetHashCode();
    public override bool Equals(object obj) => this == (Motorcycle)obj;
    public static bool operator ==(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 0;
    public static bool operator !=(Motorcycle a, Motorcycle b) => a.CompareTo(b) != 0;
    public static bool operator >(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 1;
    public static bool operator <(Motorcycle a, Motorcycle b) => a.CompareTo(b) == -1;
    public int CompareTo(Motorcycle other)
    {
        foreach (var p in sortOrders[_compareOrder])
        {
            switch (p)
            {
                case Motorcycle.Properties.Price:
                    {
                        if (Price == other.Price)
                        {
                            continue;
                        }
                        return Price.CompareTo(other.Price);
                    }
                case Motorcycle.Properties.Speed:
                    {
                        if (Speed == other.Speed)
                        {
                            continue;
                        }
                        return Speed.CompareTo(other.Speed);
                    }
                case Motorcycle.Properties.Weight:
                    {
                        if (Weight == other.Weight)
                        {
                            continue;
                        }
                        return Weight.CompareTo(other.Weight);
                    }
                case Motorcycle.Properties.Name:
                    {
                        if (Name == other.Name)
                        {
                            continue;
                        }
                        return Name.CompareTo(other.Name);
                    }
            }
        }
        return 0;
    }
}
class Motorcycle:IComparable<Motorcycle>
{
    string _name = "";
    int _speed = 0;
    int _price = 0;
    int _weight = 0;
    static int _compareOrder = 0;
    public string Name
    {
        get { return _name; }
        private set{_name = value;}
    }
    public int Speed
    {
        get { return _speed; }
        private set { _speed = value; }
    }
    public int Price
    {
        get { return _price; }
        private set { _price = value; }
    }
    public int Weight
    {
        get { return _weight; }
        private set { _weight = value; }
    }
    public static int CompareOrder
    {
        private get { return _compareOrder; }
        set
        {
            if(value >= 0 && value < sortOrders.Count)
            {
                _compareOrder = value;
            }
            else
            {
                throw new ArgumentOutOfRangeException($"Value must be between 0 and {sortOrders.Count - 1}, inclusive. Value entered is {value}.");
            }
        }
    }
    enum Properties
    {
        Name,
        Price,
        Speed,
        Weight
    }
    static readonly List<List<Properties>> sortOrders = new List<List<Properties>>()
    {
        new List<Properties>(){Properties.Price, Properties.Speed, Properties.Weight, Properties.Name },
        new List<Properties>(){Properties.Speed, Properties.Weight, Properties.Price, Properties.Name},
    };

    public Motorcycle(){}
    public Motorcycle(string name, int price, int speed, int weight)
    {
        _name = name;
        _speed = speed;
        _price = price;
        _weight = weight;
    }
    public Motorcycle(params String[] args)
    {
        _name = args[0];
        if(!int.TryParse(args[1],out _price))
        {
            throw new ArgumentException($"parameter index 1 will not parse to int.  Value is {args[1]}.");
        }
        if (!int.TryParse(args[2], out _speed))
        {
            throw new ArgumentException($"parameter index 2 will not parse to int.  Value is {args[2]}.");
        }
        if (!int.TryParse(args[3], out _weight))
        {
            throw new ArgumentException($"parameter index 3 will not parse to int.  Value is {args[3]}.");
        }
    }
    public override string ToString()
    {
        return _name;
    }
    public override int GetHashCode() => base.GetHashCode();
    public override bool Equals(object obj) => this == (Motorcycle)obj;
    public static bool operator ==(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 0;
    public static bool operator !=(Motorcycle a, Motorcycle b) => a.CompareTo(b) != 0;
    public static bool operator >(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 1;
    public static bool operator <(Motorcycle a, Motorcycle b) => a.CompareTo(b) == -1;
    public int CompareTo(Motorcycle other)
    {
        foreach (var p in sortOrders[_compareOrder])
        {
            switch (p)
            {
                case Motorcycle.Properties.Price:
                    {
                        if (Price == other.Price)
                        {
                            continue;
                        }
                        return Price.CompareTo(other.Price);
                    }
                case Motorcycle.Properties.Speed:
                    {
                        if (Speed == other.Speed)
                        {
                            continue;
                        }
                        return Speed.CompareTo(other.Speed);
                    }
                case Motorcycle.Properties.Weight:
                    {
                        if (Weight == other.Weight)
                        {
                            continue;
                        }
                        return Weight.CompareTo(other.Weight);
                    }
                case Motorcycle.Properties.Name:
                    {
                        if (Name == other.Name)
                        {
                            continue;
                        }
                        return Name.CompareTo(other.Name);
                    }
            }
        }
        return 0;
    }
}
class Motorcycle:IComparable<Motorcycle>
{
    string _name = "";
    int _speed = 0;
    int _price = 0;
    int _weight = 0;
    static int _compareOrder = 0;
    public string Name
    {
        get { return _name; }
        private set{_name = value;}
    }
    public int Speed
    {
        get { return _speed; }
        private set { _speed = value; }
    }
    public int Price
    {
        get { return _price; }
        private set { _price = value; }
    }
    public int Weight
    {
        get { return _weight; }
        private set { _weight = value; }
    }
    public static int CompareOrder
    {
        private get { return _compareOrder; }
        set
        {
            if(value >= 0 && value < sortOrders.Count)
            {
                _compareOrder = value;
            }
            else
            {
                throw new ArgumentOutOfRangeException($"Value must be between 0 and {sortOrders.Count - 1}, inclusive. Value entered is {value}.");
            }
        }
    }
    enum Properties
    {
        Name,
        Price,
        Speed,
        Weight
    }
    static readonly List<List<Properties>> sortOrders = new List<List<Properties>>()
    {
        new List<Properties>(){Properties.Price, Properties.Speed, Properties.Weight, Properties.Name },
        new List<Properties>(){Properties.Speed, Properties.Weight, Properties.Price, Properties.Name},
    };

    public Motorcycle(){}
    public Motorcycle(string name, int price, int speed, int weight)
    {
        _name = name;
        _speed = speed;
        _price = price;
        _weight = weight;
    }
    public Motorcycle(params String[] args)
    {
        _name = args[0];
        if(!int.TryParse(args[1],out _price))
        {
            throw new ArgumentException($"parameter index 1 will not parse to int.  Value is {args[1]}.");
        }
        if (!int.TryParse(args[2], out _speed))
        {
            throw new ArgumentException($"parameter index 2 will not parse to int.  Value is {args[2]}.");
        }
        if (!int.TryParse(args[3], out _weight))
        {
            throw new ArgumentException($"parameter index 3 will not parse to int.  Value is {args[3]}.");
        }
    }
    public override string ToString()
    {
        return _name;
    }
    public override int GetHashCode() => $"{_name}{_price}{_speed}{_weight}".GetHashCode();
    public override bool Equals(object obj) => this == (Motorcycle)obj;
    public static bool operator ==(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 0;
    public static bool operator !=(Motorcycle a, Motorcycle b) => a.CompareTo(b) != 0;
    public static bool operator >(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 1;
    public static bool operator <(Motorcycle a, Motorcycle b) => a.CompareTo(b) == -1;
    public int CompareTo(Motorcycle other)
    {
        foreach (var p in sortOrders[_compareOrder])
        {
            switch (p)
            {
                case Motorcycle.Properties.Price:
                    {
                        if (Price == other.Price)
                        {
                            continue;
                        }
                        return Price.CompareTo(other.Price);
                    }
                case Motorcycle.Properties.Speed:
                    {
                        if (Speed == other.Speed)
                        {
                            continue;
                        }
                        return Speed.CompareTo(other.Speed);
                    }
                case Motorcycle.Properties.Weight:
                    {
                        if (Weight == other.Weight)
                        {
                            continue;
                        }
                        return Weight.CompareTo(other.Weight);
                    }
                case Motorcycle.Properties.Name:
                    {
                        if (Name == other.Name)
                        {
                            continue;
                        }
                        return Name.CompareTo(other.Name);
                    }
            }
        }
        return 0;
    }
}
Corrected spelling
Source Link
user33306
user33306

You'll notice that I added the ToString overload. This will allow Console.Write to print out the Name of a MotorcyleMotorcycle object withwithout specifically calling the property.

You'll notice that I added the ToString overload. This will allow Console.Write to print out the Name of a Motorcyle object with specifically calling the property.

You'll notice that I added the ToString overload. This will allow Console.Write to print out the Name of a Motorcycle object without specifically calling the property.

Source Link
user33306
user33306

You seem to have some confusion about how to use Split. Since it creates an array of string, your best option is to assign that array to a variable.

For instance if your original data is in an array it could look something like this:

string[] temp = data[i].Split();

This is where having a class comes in very handy. You can use the resulting array as it is, as a collection of arguments for a constructor for your class:

public Motorcycle(params String[] args)
{
    _name = args[0];
    if(!int.TryParse(args[1],out _price))
    {
        throw new ArgumentException($"parameter index 1 will not parse to int.  Value is {args[1]}.");
    }
    if (!int.TryParse(args[2], out _speed))
    {
        throw new ArgumentException($"parameter index 2 will not parse to int.  Value is {args[2]}.");
    }
    if (!int.TryParse(args[3], out _weight))
    {
        throw new ArgumentException($"parameter index 3 will not parse to int.  Value is {args[3]}.");
    }
}

As for sorting. My thought was to create an enum of the properties:

enum Properties
{
    Name,
    Price,
    Speed,
    Weight
}

Now with a list of these properties you can iterate through it and using a switch block do the comparisons needed for a CompareTo method.

public int CompareTo(Motorcycle other)
{
    foreach (var p in sortOrders[_compareOrder])
    {
        switch (p)
        {
            case Motorcycle.Properties.Price:
                {
                    if (Price == other.Price)
                    {
                        continue;
                    }
                    return Price.CompareTo(other.Price);
                }
            case Motorcycle.Properties.Speed:
                {
                    if (Speed == other.Speed)
                    {
                        continue;
                    }
                    return Speed.CompareTo(other.Speed);
                }
            case Motorcycle.Properties.Weight:
                {
                    if (Weight == other.Weight)
                    {
                        continue;
                    }
                    return Weight.CompareTo(other.Weight);
                }
            case Motorcycle.Properties.Name:
                {
                    if (Name == other.Name)
                    {
                        continue;
                    }
                    return Name.CompareTo(other.Name);
                }
        }
    }
    return 0;
}

A 2D list of Properties allows you to have different ways of sorting. A static variable to hold the index can be used. Simply change it to point to a different list, for a different comparison.

At this point you can use this method in your sort method to compare 2 Motorcycles. My thought is go one step further and overload the comparison operators. This will simplify your sort method since it will be able to use > or < or == or != and any comparisons will be done according to the sort order that you've specified:

public static bool operator ==(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 0;
public static bool operator !=(Motorcycle a, Motorcycle b) => a.CompareTo(b) != 0;
public static bool operator >(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 1;
public static bool operator <(Motorcycle a, Motorcycle b) => a.CompareTo(b) == -1;

When it's all put together it could look like this:

class Motorcycle:IComparable<Motorcycle>
{
    string _name = "";
    int _speed = 0;
    int _price = 0;
    int _weight = 0;
    static int _compareOrder = 0;
    public string Name
    {
        get { return _name; }
        private set{_name = value;}
    }
    public int Speed
    {
        get { return _speed; }
        private set { _speed = value; }
    }
    public int Price
    {
        get { return _price; }
        private set { _price = value; }
    }
    public int Weight
    {
        get { return _weight; }
        private set { _weight = value; }
    }
    public static int CompareOrder
    {
        private get { return _compareOrder; }
        set
        {
            if(value >= 0 && value < sortOrders.Count)
            {
                _compareOrder = value;
            }
            else
            {
                throw new ArgumentOutOfRangeException($"Value must be between 0 and {sortOrders.Count - 1}, inclusive. Value entered is {value}.");
            }
        }
    }
    enum Properties
    {
        Name,
        Price,
        Speed,
        Weight
    }
    static readonly List<List<Properties>> sortOrders = new List<List<Properties>>()
    {
        new List<Properties>(){Properties.Price, Properties.Speed, Properties.Weight, Properties.Name },
        new List<Properties>(){Properties.Speed, Properties.Weight, Properties.Price, Properties.Name},
    };

    public Motorcycle(){}
    public Motorcycle(string name, int price, int speed, int weight)
    {
        _name = name;
        _speed = speed;
        _price = price;
        _weight = weight;
    }
    public Motorcycle(params String[] args)
    {
        _name = args[0];
        if(!int.TryParse(args[1],out _price))
        {
            throw new ArgumentException($"parameter index 1 will not parse to int.  Value is {args[1]}.");
        }
        if (!int.TryParse(args[2], out _speed))
        {
            throw new ArgumentException($"parameter index 2 will not parse to int.  Value is {args[2]}.");
        }
        if (!int.TryParse(args[3], out _weight))
        {
            throw new ArgumentException($"parameter index 3 will not parse to int.  Value is {args[3]}.");
        }
    }
    public override string ToString()
    {
        return _name;
    }
    public override int GetHashCode() => base.GetHashCode();
    public override bool Equals(object obj) => this == (Motorcycle)obj;
    public static bool operator ==(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 0;
    public static bool operator !=(Motorcycle a, Motorcycle b) => a.CompareTo(b) != 0;
    public static bool operator >(Motorcycle a, Motorcycle b) => a.CompareTo(b) == 1;
    public static bool operator <(Motorcycle a, Motorcycle b) => a.CompareTo(b) == -1;
    public int CompareTo(Motorcycle other)
    {
        foreach (var p in sortOrders[_compareOrder])
        {
            switch (p)
            {
                case Motorcycle.Properties.Price:
                    {
                        if (Price == other.Price)
                        {
                            continue;
                        }
                        return Price.CompareTo(other.Price);
                    }
                case Motorcycle.Properties.Speed:
                    {
                        if (Speed == other.Speed)
                        {
                            continue;
                        }
                        return Speed.CompareTo(other.Speed);
                    }
                case Motorcycle.Properties.Weight:
                    {
                        if (Weight == other.Weight)
                        {
                            continue;
                        }
                        return Weight.CompareTo(other.Weight);
                    }
                case Motorcycle.Properties.Name:
                    {
                        if (Name == other.Name)
                        {
                            continue;
                        }
                        return Name.CompareTo(other.Name);
                    }
            }
        }
        return 0;
    }
}

Now you can rework and simplify your sorting method and it will be easier to follow your logic. This will aid in debugging it.

You'll notice that I added the ToString overload. This will allow Console.Write to print out the Name of a Motorcyle object with specifically calling the property.