1

I know there's been a lot of questions and answers but neither applies to my problem. I have a jagged array that I'd like to sort by the values in array b, c and d:

string[][] toBeSorted = new string[][] { a, b, c, d, e, f, g, h, i, j};

This only sorts by "rows"

toBeSorted = toBeSorted.OrderBy(inner => inner[2]).ToArray();

I have tried Array.Sort(c.ToArray(), b) and similar approaches but they mixed the whole selection in case of multiple valued sorting

So now when I have values in toBeSorted like "John", "Smith", "New York", etc. I'd like to have my array sorted by the "cities", "surnames" and for example by the values in array g at last. So the values are a = names, b = surnames, etc.

I am .NET 3.5 dependent in my scenario.

Thank you.

6
  • 2
    Could you provide some examples? Do you want to sort values within b, c, d arrays? Commented Nov 1, 2016 at 14:26
  • Your question is not very clear - in your example "John" and "Smith" are two different arrays? what does it mean to be sorted by surname when you only have strings in your array? Commented Nov 1, 2016 at 14:30
  • 1
    It seams that each array represents some data that should sit in a class with properties.. Is that the case? Commented Nov 1, 2016 at 14:31
  • Hi @DmitryBychenko, that would do it, but the thing is I'd like to keep the other related values "linked" together. So if I sort the b,c,d arrays, the other values from a, e, f, g, h, i and j would be rearranged as well - but only b, c, d need to be sorted. Commented Nov 1, 2016 at 14:33
  • @GiladGreen, might be a way to do it, yes. Commented Nov 1, 2016 at 14:35

2 Answers 2

4

Instead of using a string[][] where each item in the inner array actually represents a different "tag" of data create a proper class with the properties being those "tags":

public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public string City { get; set; }
}

And then:

var people = new List<Person>
{
    new Person { Name = "A", LastName = "1", City = "B" },
    new Person { Name = "B", LastName = "3", City = "B" },
    new Person { Name = "C", LastName = "2", City = "B" },
};

//Linq OrderBy:
var result = people.OrderBy(p => p.LastName);

//List Sort:
people.Sort((x, y) => string.Compare(x.LastName, y.LastName));
Sign up to request clarification or add additional context in comments.

5 Comments

perfect, thanks a lot - my last question is (a little odd) - how do I extract the values from the sorted var now? from the result variable?
@baron - what do you mean extract results? It is a collection so you can iterate it
as an example when I'd like to do: foreach (var i in result) { objWriter.WriteLine(i.LastName);} ..nothing is written in the output file.
That is to do with the writing to the file itself that you are missing something. If you just foreach and print to Console you will see it is fine
Thanks a lot for all the help ;) ..I'll look deeper into it then.
3

Follwing @GiladGreen answer, you could achieve the same without creating the new classes, using anonymous types (assuming the data is uniformly formatted):

var people= toBeSorted.select (p => new {Name = p[0], LastName = p[1], City = p[2]});

var sorted = people.OrderBy(p => p.LastName);

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.