Groups | Blog | Home
all groups > c# > june 2004 >

c# : Sorted (IComparer) algo



John Wood
6/27/2004 9:19:51 PM
Your comparer implementation is problematic. You might want to try =
creating a composite key. Given the members are of type int, you can =
easily create a composite key of type long with something like the =
following:

long key =3D (row + (line * int.MaxValue));

then compare the key of what is passed in wiht the current value and =
return that.

--=20
John Wood
EMail: first name, dot, second name at priorganize.com


[quoted text, click to view]
How can i use IComparer to sort a grid of rows and lines?

This is what i need:

Shuffle Arraylist
Field ROW Field LINE
5 2
5 3
4 2
6 2
6 3
3 2
6 3
7 4
7 3
7 5


This is what i expect:

Sorted Arraylist
Field ROW Field LINE
3 2
4 2
5 2
5 3
6 2
6 3
7 3
7 4
7 5

This is the code in which i'm working (this code only sort the row =
field, but how can i sort by row (as main) and line (as secondary)


public class MainClass {
System.Collections.ArrayList objs =3D new =
System.Collectios.ArrayList();
=20
public MainClass() {
for(int row =3D 0; row < 8; row ++) {
for(int line =3D 0; line < 8; line ++) {
objs.Add(new MyClass(row,line);
}
}
objs =3D Shuffle(objs);
objs.Sort(new sortalgo);
}
}


public class MyClass
{
public row;
public line;
public MyClass(int row, int line) {
this.row=3Drow;
this.line=3Dline;
}
}

public class sortalgo : System.Collections.IComparer=20
{
public int Compare(object left, object right)
{
int row_left =3D ((MyClass)left).row;
int row_right =3D ((MyClassl)right).row;

if (row_left =3D=3D row_right)
return 0;

if (row_left < row_right)
return -1;
=20
return +1;
}
peorro77
6/27/2004 9:54:04 PM
How can i use IComparer to sort a grid of rows and lines?

This is what i need:

Shuffle Arraylist
Field ROW Field LINE
5 2
5 3
4 2
6 2
6 3
3 2
6 3
7 4
7 3
7 5


This is what i expect:

Sorted Arraylist
Field ROW Field LINE
3 2
4 2
5 2
5 3
6 2
6 3
7 3
7 4
7 5

This is the code in which i'm working (this code only sort the row =
field, but how can i sort by row (as main) and line (as secondary)


public class MainClass {
System.Collections.ArrayList objs =3D new =
System.Collectios.ArrayList();
=20
public MainClass() {
for(int row =3D 0; row < 8; row ++) {
for(int line =3D 0; line < 8; line ++) {
objs.Add(new MyClass(row,line);
}
}
objs =3D Shuffle(objs);
objs.Sort(new sortalgo);
}
}


public class MyClass
{
public row;
public line;
public MyClass(int row, int line) {
this.row=3Drow;
this.line=3Dline;
}
}

public class sortalgo : System.Collections.IComparer=20
{
public int Compare(object left, object right)
{
int row_left =3D ((MyClass)left).row;
int row_right =3D ((MyClassl)right).row;

if (row_left =3D=3D row_right)
return 0;

if (row_left < row_right)
return -1;
=20
return +1;
}
peorro77
6/27/2004 11:05:33 PM
Interesting solution to my problem, thanks.
"John Wood" <spam@isannoying.com> escribi=F3 en el mensaje =
news:uqUO83KXEHA.644@tk2msftngp13.phx.gbl...
Your comparer implementation is problematic. You might want to try =
creating a composite key. Given the members are of type int, you can =
easily create a composite key of type long with something like the =
following:

long key =3D (row + (line * int.MaxValue));

then compare the key of what is passed in wiht the current value and =
return that.

--=20
John Wood
EMail: first name, dot, second name at priorganize.com


[quoted text, click to view]
How can i use IComparer to sort a grid of rows and lines?

This is what i need:

Shuffle Arraylist
Field ROW Field LINE
5 2
5 3
4 2
6 2
6 3
3 2
6 3
7 4
7 3
7 5


This is what i expect:

Sorted Arraylist
Field ROW Field LINE
3 2
4 2
5 2
5 3
6 2
6 3
7 3
7 4
7 5

This is the code in which i'm working (this code only sort the row =
field, but how can i sort by row (as main) and line (as secondary)


public class MainClass {
System.Collections.ArrayList objs =3D new =
System.Collectios.ArrayList();
=20
public MainClass() {
for(int row =3D 0; row < 8; row ++) {
for(int line =3D 0; line < 8; line ++) {
objs.Add(new MyClass(row,line);
}
}
objs =3D Shuffle(objs);
objs.Sort(new sortalgo);
}
}


public class MyClass
{
public row;
public line;
public MyClass(int row, int line) {
this.row=3Drow;
this.line=3Dline;
}
}

public class sortalgo : System.Collections.IComparer=20
{
public int Compare(object left, object right)
{
int row_left =3D ((MyClass)left).row;
int row_right =3D ((MyClassl)right).row;

if (row_left =3D=3D row_right)
return 0;

if (row_left < row_right)
return -1;
=20
return +1;
}
Anders Borum
6/28/2004 10:23:37 AM
What happens if you want to sort on three integer fields then? Or four?

--
venlig hilsen / with regards
anders borum
--

AddThis Social Bookmark Button