Multi-dimensional foreach loop - c#

How can I properly do so that a check is performed for each string?
My code looks like :
string[,] screeny_baza = new string[300, 300];
for (int i = 0; i < 299; i++)
{
try
{
string nazwa_screna_pokolei = screeny_baza[0,i]
}
catch { };
}
As i see i want to do just it one by one. Is it possible to do it faster omitting values ​​that do not exist or have not been declared? They are just null i think.
I have dimension that looks like to 300/300 .
X0 X1 X2
Y0 00 10 20
Y1 01 11 21
Y2 02 12 22
And want to save just string to each of dimesnion for example
string [00] = "bird";
string [01] = "bird2";
and later need to get this values in loop ( omitting values ​​that do not exist or have not been declared)
Thanks for help.

I don't know about foreach loops on multi dimensional arrays but you can always do this:
string[,] screeny_baza = new string[300, 300];
for (int x = 0; x < screeny_baza.GetLength(0); x++)
{
for (int y = 0; y < screeny_baza.GetLength(1); y++)
{
try
{
string nazwa_screna_pokolei = string.empty;
if (screeny_baza[x, y] != null)
nazwa_screna_pokolei = screeny_baza[x, y];
}
catch { };
}
}

You can foreach on a 2d array. You can even LinQ Where to filter it.
var table = new string[20, 20];
table[0, 0] = "Foo";
table[0, 1] = "Bar";
foreach (var value in table.Cast<string>().Where(x =>!string.IsNullOrEmpty(x))) {
Console.WriteLine(value);
}

Actually your try-catch block will not raise any exception because when you construct the array:
string[,] screeny_baza = new string[300, 300];
you can always index it as long as the indexes are in range; so the statement:
string nazwa_screna_pokolei = screeny_baza[0,i];
will execute without error. Just nazwa_screna_pokolei will be null;
Also if speed is concerned, a nested for-loop is much faster than LinQ. at least for this simple check. for example:
var list = screeny_baza.Cast<string>().Where(x => !string.IsNullOrEmpty(x)).ToList();
will take about 10 milliseconds, but
for (int i = 0; i < 300; i++)
{
for (int j = 0; j < 300; j++)
{
if (string.IsNullOrEmpty(screeny_baza[i,j]))
{
continue;
}
list.Add(screeny_baza[i, j]);
}
}
will take only 1 millisecond.

For storing you would use row and column indices like:
screeny_baza[0,0] = "bird";
screeny_baza[0,1] = "bird";
For looping the values you use GetLength (though you know the dimensions as constant, this is a more flexible way):
for (int row = 0; row < screeny_baza.GetLength(0); row++) {
for (int col = 0; col < screeny_baza.GetLength(1); col++) {
if (!string.IsNullOrEmpty(screeny_baza[row,col])) // if there is a value
Console.WriteLine($"Value at {row},{col} is {screeny_baza[row,col]}");
}
}

My simple helper for such cases
private static void forEachCell(int size, Action<int, int> action)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
action(j, i);
}
}
}
It can be modified to use different sizes.
Usage example:
double totalProbability = 0;
forEachCell(chessboardSize, (row, col) =>
totalProbability += boardSnapshots[movesAmount][row, col]);
return totalProbability;

Related

How to pass data to a class: Error "Cannot implicitly convert type 'double' to 'CSML.Complex'

Long time lurker on this website, because I always find my answer without having to post... I cannot tell what I'm doing in this case... does someone mind to take a look and help with what I am missing?
I downloaded a linear algebra class library (CSML) that I am trying to use. I'm trying to store individual values to a Matrix class. I get the error: Cannot implicitly convert type 'double' to 'CSML.Complex'.
I've tried many different ways to initiate the Matrix and add the array:
Matrix A = new Matrix(B);
Matrix A = new Matrix(double[,] B);
I have even tried to use a for loop to add the values individually:
Matrix A = new Matrix(new double[TotalARows,TotalACols]);
for (int i = 0; i < TotalARows; i++) {
for (int j = 0; j < TotalACols; j++) {
A[i,j] = B[i,j];
}
}
Here is the code in the DLL for the creation of the Matrix Class with double[,] input
public Matrix(double[,] values)
{
if (values == null)
{
Values = new ArrayList();
columnCount = 0;
rowCount = 0;
}
rowCount = (int)values.GetLongLength(0);
columnCount = (int)values.GetLongLength(1);
Values = new ArrayList(rowCount);
for (int i = 0; i < rowCount; i++)
{
Values.Add(new ArrayList(columnCount));
for (int j = 0; j < columnCount; j++)
{
((ArrayList)Values[i]).Add(new Complex(values[i, j]));
}
}
}
Here is the 'get' 'set' of Class Complex.
public virtual Complex this[int i, int j]
{
set
{
if (i <= 0 || j <= 0)
throw new ArgumentOutOfRangeException("Indices must be real positive.");
if (i > rowCount)
{
// dynamically add i-Rows new rows...
for (int k = 0; k < i - rowCount; k++)
{
this.Values.Add(new ArrayList(columnCount));
// ...with Cols columns
for (int t = 0; t < columnCount; t++)
{
((ArrayList)Values[rowCount + k]).Add(Complex.Zero);
}
}
rowCount = i; // ha!
}
if (j > columnCount)
{
// dynamically add j-Cols columns to each row
for (int k = 0; k < rowCount; k++)
{
for (int t = 0; t < j - columnCount; t++)
{
((ArrayList)Values[k]).Add(Complex.Zero);
}
}
columnCount = j;
}
((ArrayList)Values[i - 1])[j - 1] = value;
//this.Values[i - 1, j - 1] = value;
}
get
{
if (i > 0 && i <= rowCount && j > 0 && j <= columnCount)
{
return (Complex)(((ArrayList)Values[i - 1])[j - 1]);
}
else
throw new ArgumentOutOfRangeException("Indices must not exceed size of matrix.");
}
}```
The Complex class doesn't contain an implicit conversion from double. If you want to get an instance of Complex, you need to call new Complex(_your_double_here_). You can initialize a matrix with a double array, and it will perform the conversion for you in the constructor:
var array = new double[,] {
{1,1,1,1},
{1,2,3,4},
{4,3,2,1}
};
var matrix = new Matrix(array);
// Complex has a constructor which a single 'real' value as a double
matrix[1, 2] = new Complex(3);
// It also has a constructor which a 'real' value and an 'imaginary' value
matrix[1, 2] = new Complex(3, 4);
But again, this library hasn't been updated since 2007 and will teach you some bad and obsolete practices. Have a look at Math.Net, it is up to date and has great documentation

Loop a loop inside a loop by simplifying

I've been trying to simplify the following by putting it into a loop..
int A0 = 0, A1 = 0, A2 = 0;
for (A0 = 0; A0 < nums.Length; A0++)
{
for (A1 = 0; A1 < nums.Length; A1++)
{
for (A2 = 0; A2 < nums.Length; A2++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
}
}
like A0, A1 and A2, i need to go until A75. I can get the result if i nest like the above. But how can i put it on a loop..??
I tried this one:
int[] A = new int[3];
for (int i = 0; i < A.Length; i++)
{
for (A[i] = 0; A[i] < nums.Length; A[i]++)
{
string ss = "";
for (int j = 0; j < A.Length; j++) ss += nums[A[i]];
dataGridView1.Rows.Add(new string[] { ss });
}
}
But it will perform only like..
int A0 = 0, A1 = 0, A2 = 0;
for (A0 = 0; A0 < nums.Length; A0++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
for (A1 = 0; A1 < nums.Length; A1++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
for (A2 = 0; A2 < nums.Length; A2++)
{
string ss = nums[A0] + nums[A1] + nums[A2];
dataGridView1.Rows.Add(new string[] { ss });
}
An equivalent to
public void DoNestedThings()
{
for(var A0 = 0; A0 < _max; A0 ++)
{
//...
for(var i5 = 0; i5 < _max; i5++)
{
DoThing(new List<int>{i0, ..., i5});
}
}
}
Would be:
private void DoNestedThings(int depth, Stack<int> indexes)
{
if(depth == 0)
{
DoThing(indexes);
return;
}
for(var i = 0; i < _max; i++)
{
indexes.Push(i);
DoNestedThings(depth-1, indexes);
indexes.Pop();
}
}
public void DoNestedThings()
{
DoNestedThings(5, new Stack<int>());
}
This replaces nested loops with a single loop, but then uses recursion to enter into that loop multiple times. Each time the DoNestedThings method is called with depth > 0, you enter into another loop.
(Note the order of the indexes passed to DoThing will be reversed)
See Computing a Cartesian Product with LINQ for how to write a method that can compute the Cartesian Product of a number of collections not known at compile time, which is exactly the problem you're trying to solve.
You can then create the collection of collections dynamically by simply using Repeat:
var query = Enumerable.Repeat(nums, 75)
.CartesianProduct()
.Select(combination => combination.Sum());
Of course, the size of such a query is going to be pretty large. This approach is going to leverage deferred execution to allow you to compute each result as you go, rather than computing every single result before giving you any, but if you actually try to compute a significant percentage of the values (or all of them) you're going to be waiting for...a long time.

Cannot apply indexing with [] to an expression of type 'method group'. No idea what is going on

So i'm trying to read a txt file in string theMap and then make a copy of theMap in map and return map. Also im trying to read the Txt file in 2d string. Then the returned array which is map. I want to print it at the console and there is the "Cannot apply indexing with [] to an expression of type 'method group'" problem here is the code also.
public static string[,] Reader()
{
string theMap = System.IO.File.ReadAllText(#"C:\Users\Public\Console Slayer\Map\map.txt");
int k = 0, l = 0;
string[,] map = new string[11,54];
foreach (var row in theMap.Split('\n'))
{
foreach (var col in row.Trim().Split(' '))
{
map[l,k] = (col.Trim());
l++;
}
k++;
}
return map;
}
public static void Printer()
{
for (int y = 0; y < 11; y++)
{
for (int x = 0; x < 54; x++)
{
Console.Write(Reader[y,x]);
}
Console.WriteLine();
}
}
static void Main()
{
Reader();
Printer();
}
Reader is a method. You cannot index it, but you can index the result of it:
Console.Write(Reader()[y,x]);
// ^ You need these parens to invoke the method.
However, this will invoke the function for every loop, reading the file in 11 * 54 = 594 times! Read the file once and store the result instead; there is no need to call this method on each loop iteration:
var data = Reader();
for (int y = 0; y < 11; y++)
{
for (int x = 0; x < 54; x++)
{
Console.Write(data[y,x]);
}
Console.WriteLine();
}

C# Resorting an array in a unique way

I am wanting to create multiple arrays of ints(in C#). However they all must have a unique number in the index, which no other array has that number in that index. So let me try show you what I mean:
int[] ints_array = new int[30];
for (int i = 0; i < ints_array.Count(); i++)
ints_array[i] = i;
//create a int array with 30 elems with each value increment by 1
List<int[]> arrayList = new List<int[]>();
for(int i = 0; i < ints_array.Count(); i++)
arrayList.Add(ints_array[i]. //somehow sort the array here randomly so it will be unique
So I am trying to get the arrayList have 30 int[] arrays and each is sorted so no array has the same int in the same index as another.
Example:
arrayList[0] = {5,2,3,4,1,6,7,8,20,21... etc }
arrayList[1] = {1,0,5,2,9,10,29,15,29... etc }
arrayList[2] = {0,28,4,7,29,23,22,17... etc }
So would this possible to sort the array in this unique kind of way? If you need anymore information just ask and ill fill you in :)
Wouldn't it be easier to create the arrays iteratively using an offset pattern?
What I mean is that if you created the first array using 1-30 where 1 is at index 0, the next array could repeat this using 2-30 where 2 is at index 0 and then wrap back to 1 and start counting forward again as soon as you go past 30. It would be an easy and repeatable way to make sure no array shared the same value/index pair.
You can do it like that:
List<int[]> arrayList = new List<int[]>();
Random rnd = new Random();
for (int i = 0; i < ints_array.Length; i++)
{
ints_array = ints_array.OrderBy(x => rnd.Next()).ToArray();
var isDuplicate = arrayList.Any(x => x.SequenceEqual(ints_array));
if (isDuplicate)
{
while (arrayList.Any(x => x.SequenceEqual(ints_array)))
{
ints_array = ints_array.OrderBy(x => rnd.Next()).ToArray();
}
}
arrayList.Add(ints_array);
}
I think, this wouldn't be so efficient for bigger numbers than 30.But in this case it shouldn't be a problem, in my machine it takes 7 milliseconds.
Jesse's idea would be best unless you needed a pure random pattern. In that case I would recommend generating a random number, checking all your previous arrays, and then placing it in an array if it did not match any other arrays current index. Otherwise, generate a new random number until you find a fresh one. Put that into a loop until all your arrays are filled.
Use a matrix (2D-array). It is easier to handle than a list of arrays. Create a random number generator. Make sure to initialize it only once, otherwise random number generator may create bad random numbers, if created in too short time intervals, since the slow PC-clock might not have ticked in between. (The actual time is used as seed value).
private static Random random = new Random();
Create two helper arrays with shuffeled indexes for rows and columns:
const int N = 30;
int[] col = CreateUniqueShuffledValues(N);
int[] row = CreateUniqueShuffledValues(N);
Then create and initialize the matrix by using the shuffeled row and column indexes:
// Create matrix
int[,] matrix = new int[N, N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix[row[i], col[j]] = (i + j) % N;
}
}
The code uses these two helper methods:
private static int[] CreateUniqueShuffledValues(int n)
{
// Create and initialize array with indexes.
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = i;
}
// Shuffel array using one variant of Fisher–Yates shuffle
// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm
for (int i = 0; i < n; i++) {
int j = random.Next(i, n);
Swap(array, i, j);
}
return array;
}
private static void Swap(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
int size = 10;
// generate table (no duplicates in rows, no duplicates in columns)
// 0 1 2
// 1 2 0
// 2 0 1
int[,] table = new int[size, size];
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
table[y, x] = (y + x) % size;
// shuffle rows
Random rnd = new Random();
for (int i = 0; i < size; i++)
{
int y1 = rnd.Next(0, size);
int y2 = rnd.Next(0, size);
for (int x = 0; x < size; x++)
{
int tmp = table[y1, x];
table[y1, x] = table[y2, x];
table[y2, x] = tmp;
}
}
// shuffle columns
for (int i = 0; i < size; i++)
{
int x1 = rnd.Next(0, size);
int x2 = rnd.Next(0, size);
for (int y = 0; y < size; y++)
{
int tmp = table[y, x1];
table[y, x1] = table[y, x2];
table[y, x2] = tmp;
}
}
// sample output
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
Console.Write("{0} ", table[y, x]);
Console.WriteLine();
}

How do I sort a two-dimensional (rectangular) array in C#?

I have a two-dimensional array (of Strings) which make up my data table (of rows and columns). I want to sort this array by any column. I tried to find an algorithm for doing this in C#, but have not been successful.
Any help is appreciated.
Can I check - do you mean a rectangular array ([,])or a jagged array ([][])?
It is quite easy to sort a jagged array; I have a discussion on that here. Obviously in this case the Comparison<T> would involve a column instead of sorting by ordinal - but very similar.
Sorting a rectangular array is trickier... I'd probably be tempted to copy the data out into either a rectangular array or a List<T[]>, and sort there, then copy back.
Here's an example using a jagged array:
static void Main()
{ // could just as easily be string...
int[][] data = new int[][] {
new int[] {1,2,3},
new int[] {2,3,4},
new int[] {2,4,1}
};
Sort<int>(data, 2);
}
private static void Sort<T>(T[][] data, int col)
{
Comparer<T> comparer = Comparer<T>.Default;
Array.Sort<T[]>(data, (x,y) => comparer.Compare(x[col],y[col]));
}
For working with a rectangular array... well, here is some code to swap between the two on the fly...
static T[][] ToJagged<T>(this T[,] array) {
int height = array.GetLength(0), width = array.GetLength(1);
T[][] jagged = new T[height][];
for (int i = 0; i < height; i++)
{
T[] row = new T[width];
for (int j = 0; j < width; j++)
{
row[j] = array[i, j];
}
jagged[i] = row;
}
return jagged;
}
static T[,] ToRectangular<T>(this T[][] array)
{
int height = array.Length, width = array[0].Length;
T[,] rect = new T[height, width];
for (int i = 0; i < height; i++)
{
T[] row = array[i];
for (int j = 0; j < width; j++)
{
rect[i, j] = row[j];
}
}
return rect;
}
// fill an existing rectangular array from a jagged array
static void WriteRows<T>(this T[,] array, params T[][] rows)
{
for (int i = 0; i < rows.Length; i++)
{
T[] row = rows[i];
for (int j = 0; j < row.Length; j++)
{
array[i, j] = row[j];
}
}
}
Load your two-dimensional string array into an actual DataTable (System.Data.DataTable), and then use the DataTable object's Select() method to generate a sorted array of DataRow objects (or use a DataView for a similar effect).
// assumes stringdata[row, col] is your 2D string array
DataTable dt = new DataTable();
// assumes first row contains column names:
for (int col = 0; col < stringdata.GetLength(1); col++)
{
dt.Columns.Add(stringdata[0, col]);
}
// load data from string array to data table:
for (rowindex = 1; rowindex < stringdata.GetLength(0); rowindex++)
{
DataRow row = dt.NewRow();
for (int col = 0; col < stringdata.GetLength(1); col++)
{
row[col] = stringdata[rowindex, col];
}
dt.Rows.Add(row);
}
// sort by third column:
DataRow[] sortedrows = dt.Select("", "3");
// sort by column name, descending:
sortedrows = dt.Select("", "COLUMN3 DESC");
You could also write your own method to sort a two-dimensional array. Both approaches would be useful learning experiences, but the DataTable approach would get you started on learning a better way of handling tables of data in a C# application.
Array.Sort(array, (a, b) => { return a[0] - b[0]; });
Here is an archived article from Jim Mischel at InformIt that handles sorting for both rectangular and jagged multi-dimensional arrays.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] arr = { { 20, 9, 11 }, { 30, 5, 6 } };
Console.WriteLine("before");
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write("{0,3}", arr[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("After");
for (int i = 0; i < arr.GetLength(0); i++) // Array Sorting
{
for (int j = arr.GetLength(1) - 1; j > 0; j--)
{
for (int k = 0; k < j; k++)
{
if (arr[i, k] > arr[i, k + 1])
{
int temp = arr[i, k];
arr[i, k] = arr[i, k + 1];
arr[i, k + 1] = temp;
}
}
}
Console.WriteLine();
}
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write("{0,3}", arr[i, j]);
}
Console.WriteLine();
}
}
}
}
This code should do what you are after, I haven't generalised it for n by n, but that is straight forward. That said - I agree with MusiGenesis, using another object that is a little better suited to this (especially if you intend to do any sort of binding)
(I found the code here)
string[][] array = new string[3][];
array[0] = new string[3] { "apple", "apple", "apple" };
array[1] = new string[3] { "banana", "banana", "dog" };
array[2] = new string[3] { "cat", "hippo", "cat" };
for (int i = 0; i < 3; i++)
{
Console.WriteLine(String.Format("{0} {1} {2}", array[i][0], array[i][1], array[i][2]));
}
int j = 2;
Array.Sort(array, delegate(object[] x, object[] y)
{
return (x[j] as IComparable).CompareTo(y[ j ]);
}
);
for (int i = 0; i < 3; i++)
{
Console.WriteLine(String.Format("{0} {1} {2}", array[i][0], array[i][1], array[i][2]));
}
Can allso look at Array.Sort Method http://msdn.microsoft.com/en-us/library/aa311213(v=vs.71).aspx
e.g. Array.Sort(array, delegate(object[] x, object[] y){ return (x[ i ] as IComparable).CompareTo(y[ i ]);});
from http://channel9.msdn.com/forums/Coffeehouse/189171-Sorting-Two-Dimensional-Arrays-in-C/
So your array is structured like this (I'm gonna talk in pseudocode because my C#-fu is weak, but I hope you get the gist of what I'm saying)
string values[rows][columns]
So value[1][3] is the value at row 1, column 3.
You want to sort by column, so the problem is that your array is off by 90 degrees.
As a first cut, could you just rotate it?
std::string values_by_column[columns][rows];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
values_by_column[column][row] = values[row][column]
sort_array(values_by_column[column])
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
values[row][column] = values_by_column[column][row]
If you know you only want to sort one column at a time, you could optimize this a lot by just extracting the data you want to sort:
string values_to_sort[rows]
for (int i = 0; i < rows; i++)
values_to_sort[i] = values[i][column_to_sort]
sort_array(values_to_sort)
for (int i = 0; i < rows; i++)
values[i][column_to_sort] = values_to_sort[i]
In C++ you could play tricks with how to calculate offsets into the array (since you could treat your two-dimensional array as a one-d array) but I'm not sure how to do that in c#.
Try this out. The basic strategy is to sort the particular column independently and remember the original row of the entry. The rest of the code will cycle through the sorted column data and swap out the rows in the array. The tricky part is remembing to update the original column as the swap portion will effectively alter the original column.
public class Pair<T> {
public int Index;
public T Value;
public Pair(int i, T v) {
Index = i;
Value = v;
}
}
static IEnumerable<Pair<T>> Iterate<T>(this IEnumerable<T> source) {
int index = 0;
foreach ( var cur in source) {
yield return new Pair<T>(index,cur);
index++;
}
}
static void Sort2d(string[][] source, IComparer comp, int col) {
var colValues = source.Iterate()
.Select(x => new Pair<string>(x.Index,source[x.Index][col])).ToList();
colValues.Sort((l,r) => comp.Compare(l.Value, r.Value));
var temp = new string[source[0].Length];
var rest = colValues.Iterate();
while ( rest.Any() ) {
var pair = rest.First();
var cur = pair.Value;
var i = pair.Index;
if (i == cur.Index ) {
rest = rest.Skip(1);
continue;
}
Array.Copy(source[i], temp, temp.Length);
Array.Copy(source[cur.Index], source[i], temp.Length);
Array.Copy(temp, source[cur.Index], temp.Length);
rest = rest.Skip(1);
rest.Where(x => x.Value.Index == i).First().Value.Index = cur.Index;
}
}
public static void Test1() {
var source = new string[][]
{
new string[]{ "foo", "bar", "4" },
new string[] { "jack", "dog", "1" },
new string[]{ "boy", "ball", "2" },
new string[]{ "yellow", "green", "3" }
};
Sort2d(source, StringComparer.Ordinal, 2);
}
If you could get the data as a generic tuple when you read it in or retrieved it, it would be a lot easier; then you would just have to write a Sort function that compares the desired column of the tuple, and you have a single dimension array of tuples.
This is an old question, but here's a class I just built based on the article from Jim Mischel at InformIt linked by Doug L.
class Array2DSort : IComparer<int>
{
// maintain a reference to the 2-dimensional array being sorted
string[,] _sortArray;
int[] _tagArray;
int _sortIndex;
protected string[,] SortArray { get { return _sortArray; } }
// constructor initializes the sortArray reference
public Array2DSort(string[,] theArray, int sortIndex)
{
_sortArray = theArray;
_tagArray = new int[_sortArray.GetLength(0)];
for (int i = 0; i < _sortArray.GetLength(0); ++i) _tagArray[i] = i;
_sortIndex = sortIndex;
}
public string[,] ToSortedArray()
{
Array.Sort(_tagArray, this);
string[,] result = new string[
_sortArray.GetLength(0), _sortArray.GetLength(1)];
for (int i = 0; i < _sortArray.GetLength(0); i++)
{
for (int j = 0; j < _sortArray.GetLength(1); j++)
{
result[i, j] = _sortArray[_tagArray[i], j];
}
}
return result;
}
// x and y are integer row numbers into the sortArray
public virtual int Compare(int x, int y)
{
if (_sortIndex < 0) return 0;
return CompareStrings(x, y, _sortIndex);
}
protected int CompareStrings(int x, int y, int col)
{
return _sortArray[x, col].CompareTo(_sortArray[y, col]);
}
}
Given an unsorted 2D array data of arbitrary size that you want to sort on column 5 you just do this:
Array2DSort comparer = new Array2DSort(data, 5);
string[,] sortedData = comparer.ToSortedArray();
Note the virtual Compare method and protected SortArray so you can create specialized subclasses that always sort on a particular column or do specialized sorting on multiple columns or whatever you want to do. That's also why CompareStrings is broken out and protected - any subclasses can use it for simple comparisons instead of typing out the full SortArray[x, col].CompareTo(SortArray[y, col]) syntax.
I like the DataTable approach proposed by MusiGenesis above. The nice thing about it is that you can sort by any valid SQL 'order by' string that uses column names, e.g. "x, y desc, z" for 'order by x, y desc, z'. (FWIW, I could not get it to work using column ordinals, e.g. "3,2,1 " for 'order by 3,2,1') I used only integers, but clearly you could add mixed type data into the DataTable and sort it any which way.
In the example below, I first loaded some unsorted integer data into a tblToBeSorted in Sandbox (not shown). With the table and its data already existing, I load it (unsorted) into a 2D integer array, then to a DataTable. The array of DataRows is the sorted version of DataTable. The example is a little odd in that I load my array from the DB and could have sorted it then, but I just wanted to get an unsorted array into C# to use with the DataTable object.
static void Main(string[] args)
{
SqlConnection cnnX = new SqlConnection("Data Source=r90jroughgarden\\;Initial Catalog=Sandbox;Integrated Security=True");
SqlCommand cmdX = new SqlCommand("select * from tblToBeSorted", cnnX);
cmdX.CommandType = CommandType.Text;
SqlDataReader rdrX = null;
if (cnnX.State == ConnectionState.Closed) cnnX.Open();
int[,] aintSortingArray = new int[100, 4]; //i, elementid, planid, timeid
try
{
//Load unsorted table data from DB to array
rdrX = cmdX.ExecuteReader();
if (!rdrX.HasRows) return;
int i = -1;
while (rdrX.Read() && i < 100)
{
i++;
aintSortingArray[i, 0] = rdrX.GetInt32(0);
aintSortingArray[i, 1] = rdrX.GetInt32(1);
aintSortingArray[i, 2] = rdrX.GetInt32(2);
aintSortingArray[i, 3] = rdrX.GetInt32(3);
}
rdrX.Close();
DataTable dtblX = new DataTable();
dtblX.Columns.Add("ChangeID");
dtblX.Columns.Add("ElementID");
dtblX.Columns.Add("PlanID");
dtblX.Columns.Add("TimeID");
for (int j = 0; j < i; j++)
{
DataRow drowX = dtblX.NewRow();
for (int k = 0; k < 4; k++)
{
drowX[k] = aintSortingArray[j, k];
}
dtblX.Rows.Add(drowX);
}
DataRow[] adrowX = dtblX.Select("", "ElementID, PlanID, TimeID");
adrowX = dtblX.Select("", "ElementID desc, PlanID asc, TimeID desc");
}
catch (Exception ex)
{
string strErrMsg = ex.Message;
}
finally
{
if (cnnX.State == ConnectionState.Open) cnnX.Close();
}
}
I know its late but here is my thought you might wanna consider.
for example this is array
{
m,m,m
a,a,a
b,b,b
j,j,j
k,l,m
}
and you want to convert it by column number 2, then
string[] newArr = new string[arr.length]
for(int a=0;a<arr.length;a++)
newArr[a] = arr[a][1] + a;
// create new array that contains index number at the end and also the column values
Array.Sort(newArr);
for(int a=0;a<newArr.length;a++)
{
int index = Convert.ToInt32(newArr[a][newArr[a].Length -1]);
//swap whole row with tow at current index
if(index != a)
{
string[] arr2 = arr[a];
arr[a] = arr[index];
arr[index] = arr2;
}
}
Congratulations you have sorted the array by desired column. You can edit this to make it work with other data types
Short way, but be careful of performance in big arrays and must have n-count of columns:
This will order it from the last number to the first. In case of need reverse it from 0 to "hero":
for (int i = n-1; i >= 0; i--)
{
resultsAsArray = resultsAsArray.OrderBy(x => x[i]).ToArray();
}
Example (do not be surprised, that 3 is first, 2 after that - those are negative, so -3 is lesser than -2)
var original = new int[][] { new int[] { -3, 1, 2 }, new int[] { -2, -1, 3 }, new int[] { -3, -1, 4 }, new int[] { -3, -2, 5 } };
*var processed = new int[][] { new int[] { -3, -2, 5 }, new int[] { -3, -1, 4 }, new int[] { -3, 1, 2 }, new int[] { -2, -1, 3 } };
Assuming it is a jagged array, you can use LINQ or Array.Sort() method to sort it.
Method 1: Using LINQ
var myOrderedRows = myArray.OrderBy(row => row[columnIndex]).ToArray();
Here, LINQ creates a new IEnumerable which needs to be converted to array (using ToArray()) myOrderedRows. Your original array is still unsorted. More details can be found in docs here.
Method 2: Using Array.Sort()
Array.Sort(myArray, (p, q) => p[columnIndex].CompareTo(q[columnIndex]));
In this case your original array is sorted in place. You can also provide custom Comparer for more comparison rules. More details can be found in docs here.

Categories