11

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 arraylist. I would also need to sort this arraylist by StartDate. Is this possible? Is there a better way to do it other than arraylist?

6 Answers 6

30

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Dang it...beat me to it. :-p (+1 for having faster fingers than me.)
To sort this by Startdate: var sortedList = infoList.OrderBy(x => x.StartDate)
Apparently it's in descending order so you should be using OrderByDescending().
As a rule (speaking for myself), avoid parallel arrays. Always prefer a single array of objects. So Justin's answer is right on.
How do I add items in this list ?
2

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);

4 Comments

If we are going to brag about .NET 4... why wouldn't you use 'var myData = ...'?
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.
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 =.
@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.
1

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.

Comments

1

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>();

Comments

0

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

Comments

0
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

4 Comments

MUTABLE STRUCT! Gather your women and children and get them to safety!
@Anthony Pegram: i think this is an optimization and it will increase performance in sorting actions. correct me if i'm wrong.
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. stackoverflow.com/questions/441309/why-are-mutable-structs-evil
@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.

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.