Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how can I sort Array in Object Array with element key ?

Please see picture for more info : Array in object Array

So for example in picture you can see we have 8 object now , so we want to sort 8 object with key in new array for example sort 8 object with "parentId" key in array or "categoryName" key in array !!!

But I think we have a problem because we can't access to key in new array !!!

So I think this code can't help to me :

 Array.Sort....

Now how can I sort object with array key like this ?

Kind regards

share|improve this question

3 Answers 3

up vote 2 down vote accepted

It is not obvious from your question, but the array you want to sort seems to contain dictionaries that represents object instances by having the same set of keys. To get access to the values you need to cast the array elements to the correct dictionary type (I assume it is Dictionary<String, String>). You can then use LINQ to sort by using the OrderBy clause:

var sortedByCategoryName = result
  .Cast<IDictionary<String, String>>()
  .OrderBy(d => d["categoryName"]);

The sorting will be based around string sorting because the values are strings. However, if you want to sort by say parentId (which is a number) you first need to perform a conversion:

var sortedByParentId = result
  .Cast<IDictionary<String, String>>()
  .OrderBy(d => Int32.Parse(d["parentId"]));

Note that the above code will throw exceptions if either a key is missing from the dictionary or, in the second example, if the value cannot be parsed as an integer.

share|improve this answer
    
@ Martin Liversage : Thanks for info , I like to use from your code but i have a problem !!! I Use Like this : IDictionary<XmlRpcStruct, XmlRpcStruct>>() I can't use and i have a problem with this code : .OrderBy(d => Int32.Parse(d["parentId"])); cannot convert from 'CookComputing.XmlRpc.XmlRpcStruct' to 'string' do you have any idea ? –  Sam Apr 29 '13 at 8:17
1  
@Sam: I don't know about XmlRpcStruct and the only source of information about your data structures are you screenshot. However, you could try to do results.Cast<XmlPrcStruct>(). I'm guessing the XmlRpcStruct is a dictionary-like type and perhaps the type you store in the array. –  Martin Liversage Apr 29 '13 at 8:48
    
@ Martin Liversage : YES , that is work :D thanks for help –  Sam Apr 29 '13 at 8:56

If you use Lists you can sort like this:

List<objectName> yourList = yourArray.TypeOf<objectName>().ToList();
yourList.Sort((x,y) => x.yourKey.CompareTo(y.yourKey));
share|improve this answer
    
yes but Only I can use from Array for get result ! –  Sam Apr 29 '13 at 7:22
2  
@Sam ...then put your array in a list. –  J. Steen Apr 29 '13 at 7:24
    
There you go, array converted to a list... –  Toon Casteele Apr 29 '13 at 7:27
    
@Toon Casteele & J.Steen : yeap Thanks :) :D –  Sam Apr 29 '13 at 7:34
    
you're welcome :) –  Toon Casteele Apr 29 '13 at 7:36

if you use linq you can do multiple levels of sorting

using System.Linq;

and

Array result = new Array(from item in your array order by item.parentId, item.categoryName select item);  
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.