i want to sort the array joblist by job_number property.can anyone tell me way to do it. i try to use deletage but gives error like this
Run-time exception (line 160): Failed to compare two elements in the array.
Stack Trace:
[System.NullReferenceException: Object reference not set to an instance of an object.] at RushJob.b__0(RushJob r1, RushJob r2):line 162 at System.Array.FunctorComparer 1.Compare(T x, T y) at System.Collections.Generic.ArraySortHelper
1.InsertionSort(T[] keys, Int32 lo, Int32 hi, IComparer 1 comparer) at System.Collections.Generic.ArraySortHelper
1.IntroSort(T[] keys, Int32 lo, Int32 hi, Int32 depthLimit, IComparer 1 comparer) at System.Collections.Generic.ArraySortHelper
1.IntrospectiveSort(T[] keys, Int32 left, Int32 length, IComparer 1 comparer) at System.Collections.Generic.ArraySortHelper
1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
[System.InvalidOperationException: Failed to compare two elements in the array.] at System.Collections.Generic.ArraySortHelper 1.Sort(T[] keys, Int32 index, Int32 length, IComparer
1 comparer) at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer 1 comparer) at System.Array.Sort[T](T[] array, IComparer
1 comparer) at System.Array.Sort[T](T[] array, Comparison`1 comparison) at RushJob.Main(String[] args):line 160
using System;
public class Job
{
private int _job_number;
private string _customer_name;
private string _job_description;
private int _estimated_hour;
protected int _price;
public Job(int job_number, string c_name, string j_des, int e_hour)
{
_job_number = job_number;
_customer_name = c_name;
_job_description = j_des;
_estimated_hour = e_hour;
_price = e_hour * 45;
}
public int job_number
{
get
{
return _job_number;
}
set
{
_job_number = value;
}
}
public string job_description
{
get
{
return _job_description;
}
set
{
_job_description = value;
}
}
public string customer_name
{
get
{
return _customer_name;
}
set
{
_customer_name = value;
}
}
public int get_price()
{
return _price;
}
public bool Equals(Job j)
{
if (j._job_number == this._job_number)
{
return true;
}
else
{
return false;
}
}
public override int GetHashCode()
{
return this._job_number;
}
public override string ToString()
{
return "Job Number: " + _job_number + " Customer name: " + _customer_name + " Job Description: " + _job_description + " Estimated Hours: " + _estimated_hour + " Price: " + _price;
}
}
public class RushJob : Job
{
public RushJob(int job_number, string c_name, string j_des, int e_hour) : base(job_number, c_name, j_des, e_hour)
{
this._price += 150;
}
public static void Main(string[] args)
{
RushJob[] joblist = new RushJob[5];
int job_number;
string customer_name;
string job_description;
int estimated_hour;
bool take_input = true;
int index = 0;
while (take_input)
{
Console.WriteLine("Enter Job Number");
job_number = int.Parse(Console.ReadLine());
int duplicate = 0;
for (int i = 0; i < index; i++)
{
if (joblist[i].job_number == job_number)
{
Console.WriteLine("Job Already Exists.Please Enter a new job");
duplicate = 1;
break;
}
}
if (duplicate == 1)
{
continue;
}
if (duplicate == 0)
{
Console.WriteLine("Enter Customer Name");
customer_name = Console.ReadLine();
Console.WriteLine("Enter Job Description");
job_description = Console.ReadLine();
Console.WriteLine("Enter Estimated Hour");
estimated_hour = int.Parse(Console.ReadLine());
joblist[index] = new RushJob(job_number, customer_name, job_description, estimated_hour);
Console.WriteLine("Job Added Successfully\n\n");
index++;
}
if (index == 2)
{
Console.WriteLine("Five Jobs Added Successfully\n\n");
take_input = false;
}
}
int total_price = 0;
//Comparison<RushJob> comparison = (x, y) => x.job_number.CompareTo(y.job_number);
//joblist = Array.Sort(joblist,new comparison());
Array.Sort(joblist, delegate (RushJob r1, RushJob r2)
{
return r1.job_number.CompareTo(r2.job_number);
});
for (int i = 0; i < joblist.Length; i++)
{
Console.WriteLine(joblist[i].ToString());
total_price += joblist[i].get_price();
}
Console.WriteLine(total_price);
}
}
using System.Linq;
public static void Main(string[] args)
{
var joblist = new List<RushJob>();
int job_number;
string customer_name;
string job_description;
int estimated_hour;
bool take_input = true;
int index = 0;
while (take_input)
{
Console.WriteLine("Enter Job Number");
var sJobNumber = Console.ReadLine();
if (string.IsNullOrEmpty(sJobNumber))
break;
job_number = int.Parse(sJobNumber);
if (joblist.Any(j => j.job_number == job_number))
{
Console.WriteLine("Job Already Exists. Please Enter a new job");
}
else
{
Console.WriteLine("Enter Customer Name");
customer_name = Console.ReadLine();
Console.WriteLine("Enter Job Description");
job_description = Console.ReadLine();
Console.WriteLine("Enter Estimated Hour");
estimated_hour = int.Parse(Console.ReadLine());
var job = new RushJob(job_number, customer_name, job_description, estimated_hour);
joblist.Add(job);
Console.WriteLine("Job Added Successfully\n\n");
index++;
}
}
joblist.Sort(delegate (RushJob r1, RushJob r2)
{
return r1.job_number.CompareTo(r2.job_number);
});
foreach(var job in joblist)
{
Console.WriteLine(job.ToString());
}
var total_price = joblist.Sum(j => j.get_price());
Console.WriteLine(total_price);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.