468,134 Members | 1,188 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

Sorted (IComparer) algo

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;
}
}
Nov 16 '05 #1
3 1415
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 news:ee*************@tk2msftngp13.phx.gbl...
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;
}
}
Nov 16 '05 #2
Interesting solution to my problem, thanks.
"John Wood" <sp**@isannoying.com> escribió en el mensaje news:uq*************@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 = (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 news:ee*************@tk2msftngp13.phx.gbl...
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;
}
}
Nov 16 '05 #3
What happens if you want to sort on three integer fields then? Or four?

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Robert Zurer | last post: by
10 posts views Thread by INeedADip | last post: by
12 posts views Thread by =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= | last post: by
2 posts views Thread by briankirkpatrick86 | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.