Console.WriteLine("Ascending sort with IComparer");
foreach (car c in arrayOfCars)
Console.WriteLine(c.make + "\t\t" + c.year);
Console.Read();
}
}
public class car : IComparable
{
public int year;
public string make;
public car(string Make , int Year)
{
make = Make;
year = Year;
}
int IComparable.CompareTo(object obj)
{
car c = (car)obj;
return
string.Compare(this.make , c.make);}
private class sortYearAscendingHelper:IComparer{
int IComparer.Compare(object a, object b)
{
car c1 = (car)a;
car c2=(car)b;
if (c1.year >c2.year) return 1 ;
if (c1.year <c2.year) return -1 ;
else return 0 ;
}
public static IComparer sortYearAscending()
{
return (IComparer) new sortYearAscendingHelper();
}
}
}
}
Please note that the output comes upto the default sort (including unsorted)
Even the Console.Writeline part of the Ascending Sort does not output why?
Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 92 39 Scratch Pad2
Error 2 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 103 19 Scratch Pad2
Here you are calling "sortYearAscending" from the client, but that static method comes under sortYearAscending class, it should move to car class, I tested below code and working good.
public class car : IComparable
{
public int year;
public string make;
public car(string Make, int Year)
{
make = Make;
year = Year;
}
int IComparable.CompareTo(object obj)
{
car c = (car)obj;
return string.Compare(this.make, c.make);
}
private class sortYearAscendingHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
car c1 = (car)a;
car c2 = (car)b;
if (c1.year > c2.year) return 1;
if (c1.year < c2.year) return -1;
else return 0;
}
}
public static IComparer sortYearAscending()
{
return (IComparer)new sortYearAscendingHelper();
}
}
Sorry, I didn't read all your code (could you remove some of the blank lines so that there is not so much scrolling up and down to do). Do you realise that sorting is very, very easy if you use Linq?
var sortedCars = arrayOfCars.OrderBy(c => c.Make);
var recentCarsFirst = arrayOfCars.OrderByDescending(c => c.Year);
Console.WriteLine("Ascending sort with IComparer");
foreach (car c in arrayOfCars)
Console.WriteLine(c.make + "\t\t" + c.year);
Console.Read();}}
public class car : IComparable
{ public int year;
public string make;
public car(string Make , int Year)
{make = Make;
year = Year;}
int IComparable.CompareTo(object obj)
{car c = (car)obj;
return string.Compare(this.make , c.make);}
private class sortYearAscendingHelper:IComparer{
int IComparer.Compare(object a, object b)
{car c1 = (car)a; car c2=(car)b;
if (c1.year >c2.year) return 1 ; if (c1.year <c2.year) return -1 ;
else return 0 ;} public static IComparer sortYearAscending()
{ return (IComparer) new sortYearAscendingHelper(); }}}}
Please note that the output comes upto the default sort (including unsorted)
Even the Console.Writeline part of the Ascending Sort does not output why?
Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 92 39 Scratch Pad2
Error 2 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 103 19 Scratch Pad2
Would turning so many lines of code which don't work in to a single line of working code which does the equivalent operation be sufficient incentive to learn?
Would turning so many lines of code which don't work in to a single line of working code which does the equivalent operation be sufficient incentive to learn?
Here you are calling "sortYearAscending" from the client, but that static method comes under sortYearAscending class, it should move to car class, I tested below code and working good
but you see I am still getting the same set of errors
Check your souce code again and again and at last I decide to copy your source code to test. Then I get what cause lead to this error "Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments ". This error indicates you
were using a generic Icomparer and you need to provide a type arguments for it. To avoid providing one type arguments, you need to use the full reference like below:
private class sortYearAscendingHelper:System.Collections.IComparer
Now I change your souce code and test it, works fine:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// create an array of Car objects
Car[] arrayOfCars = new Car[6]{ new Car("Merc",1992),
new Car("Toyota",1988),
new Car("Buick",1932),
new Car("Bugatti",1932),
new Car("Rolls Royce",1999),
new Car("Honda",1977)};
Console.WriteLine("unsorted output");
foreach (Car c in arrayOfCars)
{
Console.WriteLine(c.Make + "\t\t" + c.Year);
}
// default sort
Array.Sort(arrayOfCars);
Console.WriteLine("default sorted output");
foreach (Car c in arrayOfCars)
{
Console.WriteLine(c.Make + "\t\t" + c.Year);
}
// Ascending sort with IComparer
Array.Sort(arrayOfCars, Car.sortYearAscending());
Console.WriteLine("Ascending sort with IComparer");
foreach (Car c in arrayOfCars)
{
Console.WriteLine(c.Make + "\t\t" + c.Year);
}
Console.Read();
}
}
class Car : IComparable
{
string make;
int year;
public Car(string make, int year)
{
this.make = make;
this.year = year;
}
public string Make
{
get { return make; }
set { this.make = value; }
}
public int Year
{
get { return year; }
set { this.year = value; }
}
public int CompareTo(object obj)
{
if (obj == null)
{
return 0;
}
Car Car = obj as Car;
return this.year.CompareTo(Car.Year);
}
private class sortYearAscendingHelper : System.Collections.IComparer
{
int System.Collections.IComparer.Compare(object a, object b)
{
Car c1 = (Car)a; Car c2 = (Car)b;
if (c1.year > c2.year) return 1; if (c1.year < c2.year) return -1;
else return 0;
}
}
public static System.Collections.IComparer sortYearAscending()
{
return (System.Collections.IComparer)new sortYearAscendingHelper();
}
}
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
the help that I got from you sadly gets un-returned, but to make an attempt to rectify this , please let me know how to mark this as an answer given , i have already done that , as this is the way that a good answer must be responded
amigo 1
Member
60 Points
199 Posts
problem with sorting car names
Jul 18, 2012 05:33 AM|LINK
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Scratch_Pad2
{
class host
{
static void Main(string[] args)
{
// create an array of car objects
car[] arrayOfCars = new car[6]
{ new car("Merc",1992),
new car("Toyota",1988),
new car("Buick",1932),
new car("Bugatti",1932),
new car("Rolls Royce",1999),
new car("Honda",1977)
};
//unsorted output
// ----------------------------------
Console.WriteLine("unsorted output");
foreach( car c in arrayOfCars)
Console.WriteLine (c.make +"\t\t"+ c.year);
// default sort
Array.Sort(arrayOfCars);
Console.WriteLine("default sorted output");
foreach(car c in arrayOfCars)
Console.WriteLine(c.make + "\t\t" + c.year);
// Ascending sort with IComparer
Array.Sort(arrayOfCars, car. sortYearAscending());
Console.WriteLine("Ascending sort with IComparer");
foreach (car c in arrayOfCars)
Console.WriteLine(c.make + "\t\t" + c.year);
Console.Read();
}
}
public class car : IComparable
{
public int year;
public string make;
public car(string Make , int Year)
{
make = Make;
year = Year;
}
int IComparable.CompareTo(object obj)
{
car c = (car)obj;
return
string.Compare(this.make , c.make);}
private class sortYearAscendingHelper:IComparer{
int IComparer.Compare(object a, object b)
{
car c1 = (car)a;
car c2=(car)b;
if (c1.year >c2.year) return 1 ;
if (c1.year <c2.year) return -1 ;
else return 0 ;
}
public static IComparer sortYearAscending()
{
return (IComparer) new sortYearAscendingHelper();
}
}
}
}
Please note that the output comes upto the default sort (including unsorted)
Even the Console.Writeline part of the Ascending Sort does not output why?
Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 92 39 Scratch Pad2
Error 2 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 103 19 Scratch Pad2
Can you please help me with this code
Mastan Oli
Contributor
5088 Points
998 Posts
Re: problem with sorting car names
Jul 18, 2012 07:51 AM|LINK
Here you are calling "sortYearAscending" from the client, but that static method comes under sortYearAscending class, it should move to car class, I tested below code and working good.
public class car : IComparable { public int year; public string make; public car(string Make, int Year) { make = Make; year = Year; } int IComparable.CompareTo(object obj) { car c = (car)obj; return string.Compare(this.make, c.make); } private class sortYearAscendingHelper : IComparer { int IComparer.Compare(object a, object b) { car c1 = (car)a; car c2 = (car)b; if (c1.year > c2.year) return 1; if (c1.year < c2.year) return -1; else return 0; } } public static IComparer sortYearAscending() { return (IComparer)new sortYearAscendingHelper(); } }playingOOPS | மெய்ப்பொருள் காண்பதறிவு
Mark as Answer If you find helpful
Paul Linton
Star
13411 Points
2535 Posts
Re: problem with sorting car names
Jul 18, 2012 10:32 PM|LINK
Sorry, I didn't read all your code (could you remove some of the blank lines so that there is not so much scrolling up and down to do). Do you realise that sorting is very, very easy if you use Linq?
var sortedCars = arrayOfCars.OrderBy(c => c.Make);
var recentCarsFirst = arrayOfCars.OrderByDescending(c => c.Year);
You don't need any of that IComparaable stuff
Miikey
Member
21 Points
41 Posts
Re: problem with sorting car names
Jul 19, 2012 12:44 AM|LINK
Are you trying to sort them in Alphabetical order? If so its much easier.
within the column headter text include: SortExpression="columnname" at the end.
Also if your using a gv include AllowSorting="true"
I would greatly appreciate it.
amigo 1
Member
60 Points
199 Posts
Re: problem with sorting car names
Jul 19, 2012 06:14 AM|LINK
s demanded by teacher Paul Linton, below I give you the same question without the "un-necessary gaps":
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Scratch_Pad2 {class host {static void Main(string[] args)
{ // create an array of car objects
car[] arrayOfCars = new car[6]
{ new car("Merc",1992), new car("Toyota",1988), new car("Buick",1932),
new car("Bugatti",1932), new car("Rolls Royce",1999), new car("Honda",1977)};
//unsorted output
Console.WriteLine("unsorted output");
foreach( car c in arrayOfCars)
Console.WriteLine (c.make +"\t\t"+ c.year);
// default sort
Array.Sort(arrayOfCars);
Console.WriteLine("default sorted output");
foreach(car c in arrayOfCars) Console.WriteLine(c.make + "\t\t" + c.year);
// Ascending sort with IComparer
Array.Sort(arrayOfCars, car. sortYearAscending());
Console.WriteLine("Ascending sort with IComparer");
foreach (car c in arrayOfCars)
Console.WriteLine(c.make + "\t\t" + c.year);
Console.Read();}}
public class car : IComparable
{ public int year;
public string make;
public car(string Make , int Year)
{make = Make;
year = Year;}
int IComparable.CompareTo(object obj)
{car c = (car)obj;
return string.Compare(this.make , c.make);}
private class sortYearAscendingHelper:IComparer{
int IComparer.Compare(object a, object b)
{car c1 = (car)a; car c2=(car)b;
if (c1.year >c2.year) return 1 ; if (c1.year <c2.year) return -1 ;
else return 0 ;} public static IComparer sortYearAscending()
{ return (IComparer) new sortYearAscendingHelper(); }}}}
Please note that the output comes upto the default sort (including unsorted)
Even the Console.Writeline part of the Ascending Sort does not output why?
Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 92 39 Scratch Pad2
Error 2 Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments C:\Users\lenovo\documents\visual studio 2010\Projects\Scratch Pad2\host.cs 103 19 Scratch Pad2
I do not know LINQ
Paul Linton
Star
13411 Points
2535 Posts
Re: problem with sorting car names
Jul 19, 2012 06:17 AM|LINK
amigo 1
Member
60 Points
199 Posts
Re: problem with sorting car names
Jul 19, 2012 06:41 AM|LINK
Now I will
thanks
amigo 1
Member
60 Points
199 Posts
Re: problem with sorting car names
Jul 20, 2012 06:42 AM|LINK
but you see I am still getting the same set of errors
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: problem with sorting car names
Jul 24, 2012 08:40 AM|LINK
Hi,
Check your souce code again and again and at last I decide to copy your source code to test. Then I get what cause lead to this error "Using the generic type 'System.Collections.Generic.IComparer<T>' requires 1 type arguments ". This error indicates you were using a generic Icomparer and you need to provide a type arguments for it. To avoid providing one type arguments, you need to use the full reference like below:
Now I change your souce code and test it, works fine:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // create an array of Car objects Car[] arrayOfCars = new Car[6]{ new Car("Merc",1992), new Car("Toyota",1988), new Car("Buick",1932), new Car("Bugatti",1932), new Car("Rolls Royce",1999), new Car("Honda",1977)}; Console.WriteLine("unsorted output"); foreach (Car c in arrayOfCars) { Console.WriteLine(c.Make + "\t\t" + c.Year); } // default sort Array.Sort(arrayOfCars); Console.WriteLine("default sorted output"); foreach (Car c in arrayOfCars) { Console.WriteLine(c.Make + "\t\t" + c.Year); } // Ascending sort with IComparer Array.Sort(arrayOfCars, Car.sortYearAscending()); Console.WriteLine("Ascending sort with IComparer"); foreach (Car c in arrayOfCars) { Console.WriteLine(c.Make + "\t\t" + c.Year); } Console.Read(); } } class Car : IComparable { string make; int year; public Car(string make, int year) { this.make = make; this.year = year; } public string Make { get { return make; } set { this.make = value; } } public int Year { get { return year; } set { this.year = value; } } public int CompareTo(object obj) { if (obj == null) { return 0; } Car Car = obj as Car; return this.year.CompareTo(Car.Year); } private class sortYearAscendingHelper : System.Collections.IComparer { int System.Collections.IComparer.Compare(object a, object b) { Car c1 = (Car)a; Car c2 = (Car)b; if (c1.year > c2.year) return 1; if (c1.year < c2.year) return -1; else return 0; } } public static System.Collections.IComparer sortYearAscending() { return (System.Collections.IComparer)new sortYearAscendingHelper(); } } }Feedback to us
Develop and promote your apps in Windows Store
amigo 1
Member
60 Points
199 Posts
Re: problem with sorting car names
Jul 27, 2012 02:17 PM|LINK
Hello Mamba Dai
the help that I got from you sadly gets un-returned, but to make an attempt to rectify this , please let me know how to mark this as an answer given , i have already done that , as this is the way that a good answer must be responded
regards
amigo1