Sorted (IComparer) algo

P

peorro77

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 = new System.Collectios.ArrayList();

public MainClass() {
for(int row = 0; row < 8; row ++) {
for(int line = 0; line < 8; line ++) {
objs.Add(new MyClass(row,line);
}
}
objs = Shuffle(objs);
objs.Sort(new sortalgo);
}
}


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

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

if (row_left == row_right)
return 0;

if (row_left < row_right)
return -1;

return +1;
}
}
 
J

John Wood

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 = (row + (line * int.MaxValue));

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

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


"peorro77" <pedorro77.hotmail.com> wrote in message 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 = new System.Collectios.ArrayList();

public MainClass() {
for(int row = 0; row < 8; row ++) {
for(int line = 0; line < 8; line ++) {
objs.Add(new MyClass(row,line);
}
}
objs = Shuffle(objs);
objs.Sort(new sortalgo);
}
}


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

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

if (row_left == row_right)
return 0;

if (row_left < row_right)
return -1;

return +1;
}
}
 
P

peorro77

Interesting solution to my problem, thanks.
"John Wood" <[email protected]> escribió en el mensaje 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 = (row + (line * int.MaxValue));

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

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


"peorro77" <pedorro77.hotmail.com> wrote in message 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 = new System.Collectios.ArrayList();

public MainClass() {
for(int row = 0; row < 8; row ++) {
for(int line = 0; line < 8; line ++) {
objs.Add(new MyClass(row,line);
}
}
objs = Shuffle(objs);
objs.Sort(new sortalgo);
}
}


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

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

if (row_left == row_right)
return 0;

if (row_left < row_right)
return -1;

return +1;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top