473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1639
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniele Varrazzo | last post by:
If you need a container to look into, there is the sets module that provides a couple of them. If you need a sorted list, there is the bisect module. But i don't think it fits your need for a...
4
by: Robert Zurer | last post by:
Assuming that I have created a strongly typed collection and overridden the appropriate methods, i.e. this, Add, Insert etc., so that a sort order is maintained, it's still very possible for a...
1
by: Dan | last post by:
In .NET, what's the most efficient way to enumerate through all the files in a directory sorted by time (from oldest to newest)?
2
by: Raed Sawalha | last post by:
I wondering if it possilble to get the directory files ordered by creation date string files = System.IO.Directory.GetFiles(strPath,"*.msg") can I sort the returned file by creation date? ...
10
by: INeedADip | last post by:
I am trying to use a generic (reflection) IComparer class to sort a generic list but I get the error: Unable to cast object of type 'GenericComparer' to type 'System.Collections.Generic.Icomparer...
6
by: Burt | last post by:
I need to create a collection of classes (or structures) can be accessed by a string key, eg MyColl("ShortName5").Name for class with key ShortName5. But it also has to be sorted by a second...
12
by: =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= | last post by:
Hello, I´ve a problem with my Key-Value SortedList. Is there a way to sort a list (or any other data structure) by value? I have a list with 1000 key-value pairs and want to get the pairs...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
2
by: briankirkpatrick86 | last post by:
Is there an easy way to loop through directories sorted by, say, CreationTime or LastAccessTime? It seems like a reasonable capability, but I don't see it. v/r bk
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.