开发者

Sorting array of object by name (multi dimensional)

开发者 https://www.devze.com 2023-03-08 15:26 出处:网络
how can i sort a array of objects by name. Example of how the array is constructed array: object[] o = new object[items.Count+(row-exists)];

how can i sort a array of objects by name.

Example of how the array is constructed array:

     object[] o = new object[items.Count+(row-exists)];
     int i = 0;
     for(i=0; i<items.Count;i++)
     {
    XmlNode item = items[i];

    o[i] = new object[5];
    ((object[])o[i])[0] = Safe(item, "ows_Title");
    ((object[])o[i])[1] = S开发者_如何学Cafe(item, "ows_Column5");
    ((object[])o[i])[2] = ((string)Safe(item, "ows_Column7")).Trim(new char[] {'\''});
    ((object[])o[i])[3] = Convert.ToDouble(Safe(item, "ows_Column12"), provider);
    ((object[])o[i])[4] = Convert.ToDouble(Safe(item, "ows_Column9"), provider);
}

i want the 'o' to be sorted based on the value of ((object[])o[i])[0] .

Thank you


I think I understand. The linq below should do what you require.

var sortedResult = o.OrderBy(x => ((object[])x)[0]).ToArray();

However, I would look at using a differt data structure. Can you create a new type to encapsulate what the second array represents? For example:

class MyObject
{
  Safe Title {get; set;}
  Safe Column5 {get; set;}
  String Column7 {get; set;}
  double Column9 {get; set;}
  double Column12 {get; set;}
}

You can then use a SortedList to store the new objects.

0

精彩评论

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

关注公众号