0

If I have a 2D array of strings like so;

{
    {"12", "Animal", "Cat"}
    {"20", "Animal", "Dog"}
    {"6", "Vegetable", "Carrot"}
    {"5", "Mineral", "Iron"}
}

How can I use LINQ to select the data and orderby, say the 2nd 'column'? (C# linq preferably)

2
  • 1
    Arrays of arrays is a bad code smell, typically you want to create real classes and keep a list of those instead. Commented Apr 12, 2012 at 16:03
  • It's only temporary storage of a multi-delimited string Commented Apr 12, 2012 at 16:07

2 Answers 2

6

Something like:

var sorted = array.OrderBy(x => x[1])
                  .ToArray(); // Or ToList, or nothing...

It's not clear what you mean by "select the data". Personally I'd try to create a more strongly-typed data model, rather than just having an array of strings for each row. You can do that easily enough with LINQ as well, of course:

var sorted = array.Select(row => new Item {
                             Id = int.Parse(row[0]),
                             Category = row[1],
                             Name = row[2]
                          });
                  .OrderBy(x => x.Category)
                  .ToArray();

EDIT: For a query expression version of the first:

var sorted = (from x in array
              orderby x[1]
              select x).ToArray();

As you can see, it's more cluttered than using the extension methods directly. It's worth being comfortable with both forms, so you can pick the simplest one in any given situation.

Sign up to request clarification or add additional context in comments.

6 Comments

Not to nitpick, but you have an errant semicolon after your select statement.
Thanks, I was trying to do this with in-line linq rather than extension methods. Out of interest, what's the equivalent using an in-line statement?
@Rob2211: I've edited - I would advise you to use the extension method syntax for this situation.
Thanks very much for the comprehensive answer. I agree the extension methods are more elegant in this situation, the expression version was just to learn really. Thanks again.
@Jon Skeet, this way not work for me. error: 'string[,]' does not contain a definition for 'OrderBy' ...
|
0

Even if it's only temporary storage of the data, I would use an anonymous type to make the code clearer, unless the desired end result was sorted string data (i.e., if your task is to sort a text file).

Assuming you have a string[][] and not string[,], I would do this:

var query = from row in array
            let record = new { ID = int.Parse(row[0]), Category = row[1], Name = row[2] }
            orderby record.Category
            select record;

var sortedData = query.ToArray();

Extension method syntax:

var sortedData = array
    .Select(row => new { ID = int.Parse(row[0]), Category = row[1], Name = row[2] })
    .OrderBy(record => record.Category)
    .ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.