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;
}
} 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;
}
}
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;
}
}
What happens if you want to sort on three integer fields then? Or four?
--
venlig hilsen / with regards
anders borum
-- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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)?
|
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?
...
|
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...
| |
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...
|
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...
|
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...
|
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
|
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,...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |