Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you use the following code you can convert your existing dictionary of arraylists into a collection of Dictionaries and thus allowing a simple sort using Linq OrderBy</p> <pre><code>// Get the shortest arraylist length (they should be equal this is just a paranoia check!) var count=myDicList.Values.Min(x=&gt;x.Count); // Get the collection of Keys var keys=myDicList.Keys; // Perform the conversion var result=Enumerable.Range(0,count).Select(i=&gt;keys.Select(k=&gt;new {Key=k,Value=myDicList[k][i]}).ToDictionary(x=&gt;x.Key,x=&gt;x.Value)); var sorted=result.OrderByDescending(x=&gt;x["popularity"]).ToList() </code></pre> <p>-- EDIT VERSION FOR .NET 2.0</p> <p>First you need a comparer class</p> <pre><code>class PopularityComparison : IComparer&lt;Dictionary&lt;string,object&gt;&gt; { private bool _sortAscending; public PopularityComparison(bool sortAscending) { _sortAscending = sortAscending; } public int Compare(Dictionary&lt;string, object&gt; x, Dictionary&lt;string, object&gt; y) { object xValue = x["popularity"]; object yValue = y["popularity"]; // Sort Ascending if (_sortAscending) { return Comparer.Default.Compare(xValue, yValue); } else { return Comparer.Default.Compare(yValue, xValue); } } } </code></pre> <p>Then you can use the following code</p> <pre><code>// Get the shortest arraylist length (they should be equal this is just a paranoia check!) // Replacement for min int count = int.MaxValue; foreach (ArrayList a in myDicList.Values) if (a.Count &lt; count) count = a.Count; // Get the collection of Keys Dictionary&lt;string, ArrayList&gt;.KeyCollection keys = myDicList.Keys; // Perform the conversion List&lt;Dictionary&lt;string, object&gt;&gt; result = new List&lt;Dictionary&lt;string, object&gt;&gt;(count); for (int i = 0; i &lt; count; i++) { Dictionary&lt;string, object&gt; row = new Dictionary&lt;string, object&gt;(keys.Count); foreach (string key in keys) row.Add(key, myDicList[key][i]); result.Add(row); } </code></pre> <p>And then finally to sort in ascending popularity order</p> <pre><code>result.Sort(new PopularityComparison(true)); </code></pre> <p>or Descending order</p> <pre><code>result.Sort(new PopularityComparison(true)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload