Hi all,
I have scoured the internet for articles on sorting multi-dimensional
arrays of generic types e.g. T[][] using the IComparer<Tinterface
and have run into a real roadblack. There does not seem to be an easy
way to do this. I have found implementations that use the non-generic
IComparer interface but it seems to me to be more elegant to use
generics if possible.
Has anyone else had this problem and could they suggest or poin me
towards a possible solution?
Thanks in advance 5 5242
First: T[][] isn't a multi-dimensional array; it is a jagged array.
How exactly do you want to sort the data? Do you have an example?
(perhaps just using integers for simplicity...)
You can use Array.Sort on a jagged array to sort individual inner
arrays, and (separately) to sort the outer array - but you can't use
it to sort the array as a rectangle. AFAIK, even with a rectangular
array you can't do that.
Perhaps if you provide a (simple) example it will become clearer what
you want?
For example, the following sorts within each row ordinally, and sorts
the rows by the item count:
int[][] data = new int[][] {
new int[] {3,4,1},
new int[] {1,5,6,2},
new int[] {0},
new int[] {1,4}
};
for (int i = 0; i < data.Length; i++)
{
Array.Sort(data[i]);
}
Array.Sort(data, (x, y) =x.Length.CompareTo(y.Length));
for (int i = 0; i < data.Length; i++)
{
for (int j = 0; j < data[i].Length; j++)
{
Console.Write(data[i][j]);
Console.Write('\t');
}
Console.WriteLine();
}
Marc
BTW - if you need, you can get the ordinal comparer via
Comparer<T>.Default
Marc
Does that help for an example?
Yes. And btw, the language matters here, because this wouldn't be
possible with a rectangular array:
using System;
using System.Collections;
using System.Collections.Generic;
static class Program {
static void Main() {
int[][] data = new int[][] {
new int[] {1,2,3},
new int[] {2,3,4},
new int[] {2,3,1}
};
Sort<int>(data);
foreach (int[] row in data)
{
foreach (int cell in row)
{
Console.Write(cell);
Console.Write('\t');
}
Console.WriteLine();
}
}
private static void Sort<T>(T[][] data)
{
Array.Sort<T[]>(data, CompareRows<T>);
}
private static int CompareRows<T>(T[] x, T[] y)
{
int len = x.Length y.Length ? y.Length : x.Length; // max
length
IComparer<Tcomparer = Comparer<T>.Default;
for (int i = 0; i < len; i++)
{
int delta = comparer.Compare(x[i], y[i]);
if (delta != 0) return delta;
}
// if same in the overlapping portion, compare by size instead
return x.Length.CompareTo(y.Length);
}
}
On Tue, 07 Oct 2008 07:55:33 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
>Does that help for an example?
Yes. And btw, the language matters here, because this wouldn't be
possible with a rectangular array:
Minor nit: it's true that the Array class wouldn't allow direct sorting of
rows in a multi-dimensional array. But it's not hard to create a
single-dimensional array used to refer to the original array indirectly,
sort that, and then reorganize (or more likely, copy anew) the original
array based on the sorted "indirect array".
In programming, anything is possible. As long as you're willing to type
enough. :)
Pete
Thank you muchly!
I cracked it just before checking this by doing roughly the same thing
as you (except using an anonymous delegate). I like the IComparer
much more though so will adapt my solution. Thanks again!
On Oct 7, 10:55*am, Marc Gravell <marc.grav...@gmail.comwrote:
Does that help for an example?
Yes. And btw, the language matters here, because this wouldn't be
possible with a rectangular array:
using System;
using System.Collections;
using System.Collections.Generic;
static class Program {
* * static void Main() {
* * * * int[][] data = new int[][] {
* * * * * * new int[] {1,2,3},
* * * * * * new int[] {2,3,4},
* * * * * * new int[] {2,3,1}
* * * * };
* * * * Sort<int>(data);
* * * * foreach (int[] row in data)
* * * * {
* * * * * * foreach (int cell in row)
* * * * * * {
* * * * * * * * Console.Write(cell);
* * * * * * * * Console.Write('\t');
* * * * * * }
* * * * * * Console.WriteLine();
* * * * }
* * }
* * private static void Sort<T>(T[][] data)
* * {
* * * * Array.Sort<T[]>(data, CompareRows<T>);
* * }
* * private static int CompareRows<T>(T[] x, T[] y)
* * {
* * * * int len = x.Length y.Length ? y.Length : x.Length; //max
length
* * * * IComparer<Tcomparer = Comparer<T>.Default;
* * * * for (int i = 0; i < len; i++)
* * * * {
* * * * * * int delta = comparer.Compare(x[i], y[i]);
* * * * * * if (delta != 0) return delta;
* * * * }
* * * * // if same in the overlapping portion, compare by size instead
* * * * return x.Length.CompareTo(y.Length);
* * }
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: p0wer |
last post by:
Let's suppose I have this sample document:
<root>
<entry id="1"
<date>2003-08-03</date>
<param_1>5</param_1>
<param_2>10</param_2>
</entry>
<entry id="2">
...
|
by: Leszek Klich |
last post by:
Hello All !
I have a task:
QT library:
List ListBox. I generating n random numbers from range 0 to 100. I have to
sort it by hand... It has to look nicely. It's my workhome from my school....
|
by: Gareth Gale |
last post by:
I'm trying to implement a way of allowing a user to sort a HTML table
via Javascript on the client. I've seen lots of samples where single
column sorting (asc or desc) is shown, but I'd like nested...
|
by: Wing Siu |
last post by:
Dear All
I would like to develop a generic class sorting function.
Currently, when I need sort a collection of user class, for example: Person,
Exhibition, Country
I need implement a comparer...
|
by: kurt sune |
last post by:
I have a an aspx page with a gridview.
The gridview is data bound to a generic list of custom classes.
The gridview's DataSource is thus not set.
Now I want to add sorting to it.
So I create...
|
by: Harry Haller |
last post by:
Hello, I want to implement a generic list which will be used to
display 7 columns in a GridView. One should be able to sort, filter
and page each of the 7 columns. Ideally the filter should be...
|
by: > Adrian |
last post by:
In VS 2003 I could sort a column in a list view using the code below,
hover, in V2005 I get this error:
Error 1 Using the generic type 'System.Collections.Generic.IComparer<T>'
requires '1' type...
|
by: Luiz Guilherme |
last post by:
Hi,
I am a Delphi programmer and in Delphi the rows order of datagrid is given
by the sql query result, so, if I "order by" columns 1 and 2, the results
will appear ordered on the datagrid.
I...
|
by: allan.s.palmer |
last post by:
Hi everyone,
I have a generic List<that is bound to a grid view and I want to
enable sorting. I have enabled sorting on the GridView, and have set
my sort expressions for the each property of...
|
by: arnuld |
last post by:
1st I think of creating an array of pointers of size 100 as this is the
maximum input I intend to take. I can create a fixed size array but in the
end I want my array to expand at run-time to fit...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |