1

Is there an easy/simple ways to use sort of parallel arrays?. I've got 2 arrays:

    public class ApiArray
    { 
         //.. Arrays were filled (E.g. Id:{1,2,7,8,10}, Names:{name1, name2, name3, name4, name5} 
         public Array Id { get; set; }
         public Array Names { get; set; }
    }

    //..
    foreach (var N in ApiArray.Names)
    {
         listBox1.Items.Add(N);
    }

The selected Names array has to be linked to the same position in the Id array. Id array doesnt contains sequent numbers.

When the Name is selected the Id has to be used.

Thank your for suggestions.

SALMAN
  • 2,031
  • 1
  • 20
  • 18
  • @nhahtdh - .NET 4.0 introduced the [`Tuple`](http://msdn.microsoft.com/en-us/library/system.tuple.aspx) classes for this, though they suffer from readability. – Oded Jul 14 '12 at 13:41
  • @Oded: I don't code in C#, but it seems that it has some interesting functions. – nhahtdh Jul 14 '12 at 13:42
  • @nhahtdh - C# (and .NET in general) does have some very nice features. – Oded Jul 14 '12 at 13:43

5 Answers5

3

Use Array.Sort(keys,items) to sort array items on keys from array keys.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

If Id and Name belong to the same object, you should group them in a class:

class IdName
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public IdName myArray[];
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
1

Is there a reason why you can't break out Id and Name into a class of its own?

public class MyObject
{
    public string ID;
    public string Name;
}

public class ApiArray
{
    public List<MyObject> MyObjects;
}
skaz
  • 21,962
  • 20
  • 69
  • 98
0

You have to make

Class User 
{  
  String name;int id;  
  User(name,id){this.id=id; this.name=name;} 
}

Now you have to maintain Single Array each Array index consist of User Object incase if you sort this array whole objects will changes their index position . Thanks

Oded
  • 489,969
  • 99
  • 883
  • 1,009
SALMAN
  • 2,031
  • 1
  • 20
  • 18
0

If you have more than 2 parallel arrays it might be useful to create a temp copy of the one you're sorting them on, recopying it from the original each time you run the sort function, except for the last one of course. Below is a snippet from a program I'm working on for school. The arrays are declared and initialized elsewhere but hopefully you get the idea.

string[] tempCustomerName = new string[CustomerName.Length];

CustomerName.CopyTo(tempCustomerName, 0);
Array.Sort(tempCustomerName, Id);

CustomerName.CopyTo(tempCustomerName, 0);
Array.Sort(tempCustomerName, CustomerDiscountCode);

Array.Sort(CustomerName, CustomerRate);

Also, probably better to use a class to encapsulate the arrays and then make a collection of that class, much simpler to sort. Something like this:

Public class Customer
{
  public string Name {get; set;}
  public int Id {get; set;}
  public char DiscountCode {get; set;}
  public decimal Rate {get; set;}
}

Add customers to the list and then sort like this:

List<Customer> SortedList = objListOrder.OrderBy(o=>o.Name).ToList();*

*Borrowed from https://stackoverflow.com/a/3309230/5676505