0

I have this arrays:

int[] a = {5, 2, 3}; 
int[] b = {4, 1, 2}; 
string[] c = {"John", "Peter", "Max"};

I need to sort the values ​​of, say, the second array (b[]), but in relation with the other two arrays, so that they get sorted accordingly. For example, 1 will come first as it's the lowest number, and since 1 in b[] relates to 2 in a[] and "Peter" in c[], then it means that 2 and "peter" will also move to the first sort position too. The same goes for the other two columns; observe:

int[] a = {2, 3, 5}; 
int[] b = {1, 2, 4}; 
string[] c = {"Peter", "Max", "John"};

How would I do this?

4
  • 3
    Your question isn't quite clear. Are you trying to sort one array based on the values of another? Commented Jun 1, 2012 at 19:36
  • Are you just asking how to sort an array? Commented Jun 1, 2012 at 19:36
  • 1
    My assumption is that he wants to effectively sort a grid by sorting one of the columns of that grid. Commented Jun 1, 2012 at 19:37
  • Why don't you use a DataTable for this purpose where a,b and c are DataColumns of type int,int,string? Commented Jun 1, 2012 at 19:45

4 Answers 4

8

I think I understand what you are saying. You want to sort array a and c based on the values of b.

You can sort an array by another array using Array.Sort which lets you specify another array for keys for example:

int[] a = { 5, 2, 3 }; 
int[] b = { 4, 1, 2 };
string[] c = { "John", "Peter", "Max" };

Array.Sort(b.ToArray(), c);
Array.Sort(b.ToArray(), a);
Array.Sort(b);
Console.WriteLine(string.Join(", ", a));
Console.WriteLine(string.Join(", ", b));
Console.WriteLine(string.Join(", ", c));

This will output your expected values. Note that we use ToArray to create a copy of the arrays when sorting by key, that's because Array.Sort sorts both they keys and the values, which we don't want. We don't sort the Keys (b in this case) till the end.

That's how we solve your immediate problem. However, gathered from the comments, you are trying to sort tabular data. When your data has some structure to it, say like this:

public class Item
{
    public int A { get; set; }
    public int B { get; set; }
    public string C { get; set; }
}

It gets a lot easier.

var items = new[]
{
    new Item {A = 5, B = 4, C = "John"},
    new Item {A = 2, B = 1, C = "Peter"},
    new Item {A = 3, B = 2, C = "Max"},
};
var sortedItems = items.OrderBy(i => i.B).ToArray();

This uses LINQ, which is perfect for what you are trying to do.

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

Comments

1

Instead of storing your data in seperate unassociated arrays, you might want to look at Tuples, or if you can, using a dedicated class to keep the data linked together

Comments

1

Can you consolidate the arrays such that each item represents a whole record?

// using anonymous types for simplicity
var source = new[] {
    new { a = 5, b = 4, c = "John" },
    new { a = 2, b = 1, c = "Peter" },
    new { a = 3, b = 2, c = "Max" }
};
var results = source.OrderBy(x => x.a);

Comments

0
var indices = Enumerable.Range (0, a.Length).ToArray ();
var sortedIndices = indices.OrderBy (j => a [j]);

var i = 0;
foreach (var j in sortedIndices) {
   anew [i] = a [j];
   bnew [i] = b [j];
   cnew [i] = c [j];
   i++;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.