开发者

How do I sort an ArrayList containing more than 1 parameter per object alphabetically?

Greetings (Don't know if the title makes sense)

I have an ArrayList which can contain a different amount of objects in it.

Example

private ArrayList items = new ArrayList();
var list = web.Lists[ListName];

items.Add(new { GroupName = value });

foreach (SPListItem item in list.Items)
{
   items.Add(new { GroupName = value, ItemID = item.ID, ItemName = item.Name });
}

As you can see above, I hav开发者_如何学Ce an object only containing GroupName and then an object containing GroupName, ItemID and ItemName

What I want to do is to sort all the items by their ItemName.

I have no issue sorting it when it's only one parameter, but when it's multiple parameters, I'm clueless. How do I do this?


EDIT: Sort before adding to the new arraylist

foreach(SPListItem item in list.Items.Cast<SPListItem>.OrderBy(i => i.Name))
{
   items.Add(new { GroupName = value, ItemID = item.ID, ItemName = item.Name });
}

Newer EDIT: If you just want an enumerable collection (without the arraylist, in which you cannot extract the items from easily), you could just use the LINQ Select operator.

var items = ist.Items.Cast<SPListItem>.OrderBy(i => i.Name).Select(i => new { GroupName = value, ItemID = i.ID, ItemName = i.Name });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜