2

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] = Safe(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

6
  • Can't get you question. Could you please ellaborate? Commented May 27, 2011 at 11:13
  • the array 'o' contains objects with a new array, am I right? I want the 'o' to be sorted based on the value/(string) of the index 0 of the 2nd array. I'm not sure if I managed to explain myself any better. Commented May 27, 2011 at 11:16
  • 2
    Why don't you store your object in generic SortedList, with object's primary key as list's key or choose key as per your requirements? Commented May 27, 2011 at 11:18
  • Still can't get your requirement. Commented May 27, 2011 at 11:19
  • So o will be sorted differently on each iteration of i? Commented May 27, 2011 at 11:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks IndigoDelta, looks like you understand what I'm in need of. unfortunately I don't have access to linq, and I'm on a 2.x framework.
I pity the man without access to linq. You can write your own sorting algorithm if required. Have a look some examples. There is a library on CodeProject of sorting algorithms which might help you

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.