Please how do I sort the following array (in C#) by the second column desc, then by the first column asc (and then insert the resulting rank in the third column which I guess might be a separate question).
I'm a beginner in C#, I have spent quite some time looking for an answer to this seemingly simple question, but I couldn't find any approach that would work for me. Any help is much appreciated, thanks.
int[,] myArray = new int[5, 4]
{
{1, 7, 0, 0} ,
{2, 12, 0, 0} ,
{3, 15, 0, 0} ,
{4, 7, 0, 0} ,
{5, 1, 0, 0}
};
In a multidimensional array, there isn’t really a concept of “rows”. All values are just in the same relation to another. That’s why sorting multidimensional arrays is somewhat tricky.
It gets easier when you use jagged arrays, basically arrays of arrays. Your array would look like this:
int[][] myJaggedArray = new int[][]
{
new int[] { 1, 7, 0, 0 },
new int[] { 2, 12, 0, 0 },
new int[] { 3, 15, 0, 0 },
new int[] { 4, 7, 0, 0 },
new int[] { 5, 1, 0, 0 },
};
You can also convert your multidimensional array into a jagged array, doing something like this:
int[][] myJaggedArray = new int[myArray.GetUpperBound(0) + 1][];
for(int i = 0; i < myJaggedArray.Length; i++)
myJaggedArray[i] = Enumerable.Range(0, myArray.GetUpperBound(1) + 1).Select(k => myArray[i,k]).ToArray();
Once you have a jagged array, sorting it is really easy using LINQ:
var result = myJaggedArray
.OrderBy(row => row[1])
.ThenBy(row => row[0])
.Select((row, idx) =>
{
row[2] = idx;
return row;
})
.ToArray();
Related
Given two Lists of integer arrays of the form:
genData = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}};
orgData = {{1,2,3,4}, {1,2,3,4}, {2,4,6,8}, {1,2,3,4}};
I'd like to determine if the sum of two subarrays at the same index in both lists don't match. If the sums match, do nothing. If the sums don't match, convert every integer in both subarrays into a 0.
For example, in the two lists above the subarrays at index 2 have a non-matching sum (10 vs 20). I'd like to convert the lists to
genData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
I'm trying to first create a list if sums by trying
var genDataSum = genDataList.ForEach(x => x.Sum());
But obviously, that's throwing up errors..."Cannot assign void to an implicitly typed value".
Any help or guidance will be greatly appreciated.
You need to use select to get the sum. list.foreach works like normal for loop.
List<int[]> genData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 }
};
List<int[]> orgData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 2, 4, 6, 8 },
new int[] { 1, 2, 3, 4 }
};
var sumsGenData = genData.Select(a => a.Sum()).ToList();
var sumsOrgData = orgData.Select(a => a.Sum()).ToList();
for (int i = 0; i < sumsGenData.Count; i++)
{
if (sumsGenData[i] != sumsOrgData[i])
{
orgData[i] = new int[] { 0, 0, 0, 0 };
}
}
ForEach doesn't return anything. Use Select.
var orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
var sums = orgData.Select( a => a.Sum() );
I am wondering how to get the row of the two dimensional array like
int[3,3] a = ****
I tried a[0], but it failed. anyway to get the rwo array?
Two-dimensional array is not array of arrays. If you want to get 'row', then you need to get all items from array which have same value of some dimension. E.g. if you are getting all values from first dimension:
int[,] array = new int[4, 3] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
for(int i = 0; i <= array.GetUpperBound(1); i++)
Console.WriteLine(array[0,i]); // getting all items from first dimension
You can put all these items to array:
int rowIndex = 0;
int[] row = Enumerable.Range(0, array.GetUpperBound(1) + 1)
.Select(i => array[1, i])
.ToArray();
Another option will be using jagged-array instead of two-dimensional array:
int[][] array = new []{
new[] { 1, 2, 3 },
new[] { 4, 5, 6 },
new[] { 7, 8, 9 },
new[] { 10, 11, 12, 13, 14, 15 }
};
It's an array of arrays. But note that inner arrays can have different size. Getting some 'row' will look like
int[] row = array[0];
In python there is a functionality (numpy.take) to sort arrays within an array, for example if I have an array (3x3):
a = [[1, 2, 3],[7,9,10],[3, 5,6]]
and I have an array of set indices
indices = [2, 0, 1]
the result shall be
array([[ 3, 5, 6], [ 1, 2, 3], [ 7, 9, 10]]).
Are there any direct approach methods/ functions as these in C# where I can pass in a jagged array and produce the same output?
Not directly, but you can achieve the same thing with Linq
var a = new[] { new[] { 1, 2, 3 }, new[] { 7, 9, 10 }, new[] { 3, 5, 6 } };
var indices = new [] { 2, 0, 1 };
var sorted = indices.Select(i => a[i]).ToArray();
foreach(var s in sorted) Console.WriteLine(string.Join(", ", s));
Note this does not check that your indices are all in range.
You can do it easily with LINQ:
var a = new[] { new[] { 1, 2, 3 }, new[] { 7, 9, 10 }, new[] { 3, 5, 6 } };
var indices = new[] { 2, 0, 1};
var result = indices
.Select(i => a[i])
.ToArray();
Or .ToList() if you prefer lists.
There is also the Array.Sort(keys, values) - MSDN
var a = new[]
{
new[] {1, 2, 3},
new[] {7, 9, 10},
new[] {3, 5, 6}
};
var indices = new[] {2, 0, 1};
var sortedArray = a.SortEx(indices);
Where SortEx is
public static class Extensions
{
public static T[][] SortEx<T>(this T[][] source, int[] indices)
{
return indices.Select(index => source[index]).ToArray();
}
}
This assumes that the all the indices in the indices array are not out of bound in a.
Let's say you have two arrays, src and dest. dest is bigger. You want to copy over all the elements from src to the beginning of dest and overwrite anything that may already be there. To do this, we can do an Array.Copy(src, dest, src.Length), which is both more concise and more efficient than a for loop.
Now, say dest becomes a List<T>. What's the most efficient way to copy all of the elements from src? I know List is internally implemented using an array, so if we could get our hands on that (barring reflection, of course) we could just do an Array.Copy and this would be a non-issue.
I'm not looking for a for-loop because of said reasons, but if that's the only way to do it I guess that'll have to do.
Edit: I was hoping not to have to type up a code sample on my phone, but it seems from the barrage of incorrect answers I'm going to have to:
int[] src = { 1, 2, 3 };
var dest = new List<int>() { 4, 5, 6, 7 };
for (int i = 0; i < src.Length; i++)
{
dest[i] = src[i];
}
What I'm looking for would be the logical equivalent of the above, but have the performance of Array.Copy.
To achieve the same result as Array.Copy I would use
var src = new int[] { 50, 51, 52, 53, 54, 55 };
var dest = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
dest.RemoveRange(0, src.Length);
dest.InsertRange(0, src);
// dest: [ 50, 51, 52, 53, 54, 55, 7, 8, 9, 10, 11, 12 ]
Edit:
This method is about 7 times slower than Array.Copy, but for large Arrays much faster than to loop through the array. If you have smal source arrays, a loop may be the best choice.
I think you are looking for something like this:
int[] desta = (int[]) typeof(List<int>)
.GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(dest);
Array.Copy(src, desta, src.Length)
Why can't you just call ToList() on src array which will Create a List<int> from int[]. A small sample
int[] src = new int[] { 1,2,3,4,5,6,7,8,44,555,45,654};
List<int> dest = src.ToList();
You can also do
int[] src = new int[] {1, 2, 3};
List<int> dest = new List<int>(src);
If your src is a list, another option is to use ForEach defined in List, I believe the performance is similar to Array.Copy
dest = new List<T>();
src.ForEach(item => dest.Add(item));
If you need value copy, you can write this:
src.ForEach(item => dest.Add(item.Clone()));
In this case, just need to make sure item is ICloneable.
Just compared your function with the Array.Copy() function.
//values used for the benchmark
int[] src = { 1, 2, 3 };
int[] destArray = { 4, 5, 6, 7 };
var destList = new List<int>() { 4, 5, 6, 7 };
//Array.Copy() test : avarage 1004 ms
for (int i = 0; i < 20000000; i++)
{
Array.Copy(src, destArray, src.Length);
}
//Your solution test : avarage 634 ms
for (int i = 0; i < 20000000; i++)
{
Copy(src, destList, src.Length);
}
public void Copy(int[] sourceArray, List<int> destinationList, int length)
{
for (int i = 0; i < length; i++)
{
destinationList[i] = sourceArray[i];
}
}
*these results are the avarage of 20 benchmarks.
I have 2D array in c#, like this:
int[][] 2darray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
how can I get one column as normal array, like
int[] array = 2darray[1][]; //example, not working
and have
int[] array = {3,4};
?
Thanks.
There are several reasons why your code can't compile
This way it works:
int[][] array2d = { new[]{ 1, 2 }, new[]{ 3, 4 }, new[]{ 5, 6 }, new[]{ 7, 8 } };
int[] array = array2d[0];
Problems:
2darray is not a valid variable name
The indexing is wrong
The initialization of the original array is wrong
EDIT:
As stated by #heltonbiker, if you require all elements of the first column, you can use this:
int[] col = array2d.Select(row => row[0]).ToArray();
For an array with two columns and four rows, you can use LINQ this way:
using System.Linq;
first_column = _2darray.Select(row => row[0]).ToArray();
Note that changing the first or second array will not change the other one.
You are confusing jagged arrays and multidimensional arrays in C#. While they are similar, there is a slight difference. Rows in a jagged array can have a different number of elements, while in a 2D-array they are of the same length. Therefore when working with jagged arrays you need to remember to write handling for a missing column element. I composed a sample console app below to show how both of them work - it uses 0 as a substitute for a missing element, but you can throw an error etc.:
using System.Collections.Generic;
namespace JaggedArrayExample
{
class Program
{
static void Main(string[] args)
{
//jagged array declaration
int[][] array1;
//jagged array declaration and assignment
var array2 = new int[][] {
new int[] { 1, 2 },
new int[] { 3, 4 },
new int[] { 5, 6 },
new int[] { 7, 8 }
};
//2D-array declaration
int[,] array3;
//2D-array declaration and assignment (implicit bounds)
var array4 = new int[,] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
//2D-array declaration and assignment (explicit bounds)
var array5 = new int[4, 2] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
//get rows and columns at index
var r = GetRow(array2, 1); //second row {3,4}
var c = GetColumn(array2, 1); //second column {2,4,6,8}
}
private static int[] GetRow(int[][] array, int index)
{
return array[index]; //retrieving the row is simple
}
private static int[] GetColumn(int[][] array, int index)
{
//but things get more interesting with columns
//especially if jagged arrays are involved
var retValue = new List<int>();
foreach (int[] r in array)
{
int ub = r.GetUpperBound(0);
if (ub >= index) //index within bounds
{
retValue.Add(r[index]);
}
else //index outside of bounds
{
retValue.Add(0); //default value?
//or you can throw an error
}
}
return retValue.ToArray();
}
}
}
try this, it should work
int[] array = array2d[1];
Change the name of the variable to array2d, you cannot have variable that starts with number, a variable can start with letter or underscore.