tags:

views:

171

answers:

6

Is it possible to create a multidimensional arraylist in C#?

StartDate   Qty   size
9/1/2010    10     15
9/1/2009    12     17
9/1/2008    11     19

StartDate , Qty and size are the 3 arraylists. I need to have them in a single array list. I would also need to sort this arraylist by startdate. Let me know if this possible? Is there a better way to it other than arraylist

Thanks Prady

+19  A: 

You can do it that way, yes. But in this case since each row seems related, why not create a class to hold the data:

public class Info
{
    public DateTime StartDate { get; set; }
    public int Qty { get; set; }
    public int Size { get; set; }
}

And then just have a regular List to hold the objects:

List<Info> infoList = new List<Info>();

That will save you from having to worry about the ordering of each List. To handle the ordering, you can use the LINQ to Objects Extension methods:

var sortedList = infoList.OrderBy(i => i.StartDate);
Justin Niessner
Dang it...beat me to it. :-p (+1 for having faster fingers than me.)
JasCav
To sort this by Startdate: var sortedList = infoList.OrderBy(x => x.StartDate)
Loki Stormbringer
Apparently it's in descending order so you should be using `OrderByDescending()`.
Jeff M
As a rule (speaking for myself), avoid parallel arrays. Always prefer a single array of objects. So Justin's answer is right on.
Detmar
+1  A: 

You want a list of lists. No reason to use ArrayList either. From your example:

List<List<DateTime>> list = new List<List<DateTime>>();

That said, I prefer something like Justin has shown above.

Ed Swangren
+1  A: 

If these properties describe an entity or "data row", you might consider creating a class:

public class MyClass
{
    public DateTime StartDate {get;set;}
    public int Qty {get;set;}
    public int Size {get;set;}
}

You can then create an array of these objects:

List<MyClass> myClassArray = new List<MyClass>();
Dave Swersky
A: 

You could use a SortedList<> and have an object added to it that has the 3 List<> instances. Then you need to write a comparer to sort your objects by StartDate

Dimitris
+1  A: 

When you can, go with Justin's answer. It will save headaches in the long run because each property has meaning. If you need a quick approach and you have .NET 4, you could list the Tuple in a list. Such as

List<Tuple<DateTime, int, int>> myData = new List<Tuple<DateTime, int, int>>();
myData.Add(new Tuple<DateTime, int, int>(DateTime.Now, 1, 2));

//

DateTime myDate = myData[0].Item1;
int myQty = myData[0].Item2;
int mySize = myData[0].Item3;

If you do not have .NET 4, it is trivial to implement your own tuple. However, if you are going to do that, might as well skip back to the top and go with Justin's answer.

Edit For completeness, here are sorting options using this approach.

// descending sort done in place using List<>.Sort
myData.Sort((t1, t2) => -1 * t1.Item1.CompareTo(t2.Item1)); 

// descending sort performed as necessary in a sequence
var orderedList = myData.OrderByDescending(t => t.Item1);
Anthony Pegram
If we are going to brag about .NET 4... why wouldn't you use 'var myData = ...'?
WernerCD
I don't particularly like `var` and generally only use it when dealing with queries (and sometimes not even then). For all else, I prefer to be explicit at all times.
Anthony Pegram
I understand that in some cases, explicit is better... Guess its just preference that there is no need to type out List<tuple....>> twice - once on each side of the =.
WernerCD
@Werner, yep, it's just a preference. But to play devil's advocate, you still only type it *once*, intellisense carries the burden on the right side.
Anthony Pegram
A: 
public struct MyStruct
{
    public DateTime StartDate { get; set; }
    public int Qty { get; set; }
    public int Size { get; set; }
}
...
List<MyStruct> MyList = new List<MyStruct>();
...
MyList.Sort((item1, item2) => item1.Qty.CompareTo(item2.Qty)); //ascending sort
...
MyList.Sort((item1, item2) => item2.Qty.CompareTo(item1.Qty)); //descending sort
syntaxcheck
MUTABLE STRUCT! Gather your women and children and get them to safety!
Anthony Pegram
@Anthony Pegram: i think this is an optimization and it will increase performance in sorting actions. correct me if i'm wrong.
syntaxcheck
I have no idea about the performance of struct versus class in a sorting situation except to say test it and see in various scenarios, all of which I can't go into. But my comment refers to the fact that mutable structs are roundly considered evil. See this question and the answers and linked blog posts, etc., for more on that. http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil
Anthony Pegram
@Anthony: absolutely you are right. i have tested both of them in some different situation. the result was interesting. when using properties with primary types, struct list is a little faster in sort actions. but when using other types in properties, there is not much difference. but in both situations adding a bunch of huge data into struct list is also a little faster than class list.
syntaxcheck