简体   繁体   中英

How do I create a multidimensional array list in c#?

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?

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

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM