Reputation: 13
My code is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1CS
{
class Program
{
private static List<string[]> list;
//private static string input;
static void Main(string[] args)
{
list = new List<string[]>();
int n = 2;
//------PROBLEM IS HERE::
string[,] Ar = new string[n,3];
Ar[0,0] = "A";
Ar[0,1] = "133";
Ar[0,2] = "2";
list.Add(Ar[0]);
Ar[1,0] = "d";
Ar[1,1] = "4";
Ar[1,2] = "2";
list.Add(Ar);
//---------------------
//sort
var sum = from s in list orderby (Int32.Parse(s[1]) + Int32.Parse(s[2])) select s;
//change for array
Array sumn = sum.ToArray();
foreach (String[] s in sumn)
{
String result = String.Join(" ", s);
Console.WriteLine(result);
}
}
}
}
I want to create a list of string arrays. As you can see in each row of array Ar there is:
0 column is name of point
1 column is x coordinate
2 column is y coordinate
I want to sort points in ascending order with respect to their sum of coordinates. Is there an option to use list of arrays to solve this problem(yes/no and why)? If there isn't pls give me another solution.
Upvotes: 0
Views: 286
Reputation: 20090
in your case you should avoid using a array of string since you could have 1 issue. what if some some reason index 1 and 2 of the array are not parsable to int?
also using a class would explain exactly what are the value, also see what Servy said.
you asked for another solution so here is one that seem to work with the information that you gave.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var list = new List<Coord>();
list.Add(new Coord() { Name = "a", X = 133, Y = 2 });
list.Add(new Coord() { Name = "d", X = 4, Y = 2 });
var sum = list.OrderBy(x => x.X + x.Y);
foreach(var s in sum)
{
Console.WriteLine(s.ToString(" "));
}
Console.Read();
}
}
class Coord
{
public string Name { get; set; }
public int X { get; set; }
public int Y { get; set; }
public string ToString(string sep = "")
{
return string.Format("{0}{3}{1}{3}{2}", Name, X, Y, sep);
}
}
}
Upvotes: 0
Reputation: 6403
You can use a List<string[]>
List<string[]> rows = new List<string[]>();
string[] ar = new string[3];
ar[0] = "A";
ar[1] = "133";
ar[2] = "2";
rows.Add(ar);
/*
be advised that you can't just go further without creating a new array,
as you did in your code
*/
ar = new string[3];
ar[0] = "D";
ar[1] = "4";
ar[2] = "2";
rows.Add(ar);
... altough I would heavily suggest using structures instead, as they're cleaner, faster and easier to use.
Using struct
, it would look like this:
struct Row {
string name;
int x;
int y;
}
List<Row> rows = new List<Row>();
Row r = new Row();
r.name = "A";
r.x = 133;
r.y = 2;
rows.Add(r);
r = new Row();
r.name = "B";
r.x = 4;
r.y = 2;
rows.Add(r);
Upvotes: 0
Reputation: 203802
Rather than having a two dimensional array in which one of the dimensions is used to determine which value of a single logical object is used, you should instead create a new class
. Give that class three properties, and create a single dimensional data structure (whether it be an array or a list) of objects of that new custom type.
Upvotes: 1