<p>What is the best algorithm to take array like below:</p>

<p><code>A {0,1,2,3}</code></p>

<p>I expected to order it like array below:</p>

<p><code>B {3,1,0,2}</code>  </p>

<p>Any ideas?</p>

[{'answer_id': 44277, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': 3, 'selected': False, 'text': '<p>So if you have two arrays and they hold the same data just in different order then just do this:</p>\n\n<p>A = B</p>\n\n<p>I suspect that is not your situation so I think we need more info.</p>\n'}, {'answer_id': 44282, 'author': 'harpo', 'author_id': 4525, 'author_profile': 'https://Stackoverflow.com/users/4525', 'pm_score': 1, 'selected': False, 'text': '<p>In the example you gave (an array of numbers), there would be no point in re-ordering A, since you could just use B.</p>\n\n<p>So, presumably these are arrays of objects which you want ordered by one of their properties.</p>\n\n<p>Then, you will need a way to look up items in A based on the property in question (like a hashtable).  Then you can iterate B (which is in the desired sequence), and operate on the corresponding element in A.</p>\n'}, {'answer_id': 44284, 'author': 'JC Grubbs', 'author_id': 4541, 'author_profile': 'https://Stackoverflow.com/users/4541', 'pm_score': 1, 'selected': False, 'text': '<p>Both array\'s contain the same values (or nearly so) but I need to force them to be in the same order.  For example, in array A the value "3045" is in index position 4 and in array B it is in index position 1.  I want to reorder B so that the index positions of like values are the same as A.</p>\n'}, {'answer_id': 44296, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': 1, 'selected': False, 'text': '<p>If they are <em>nearly</em> the same then here is some pseudo code:</p>\n\n<pre><code>Make an ArrayList\nCopy the contents of the smaller array to the arraylist\nfor each item I in the larger array\n    FInd I in the ArrayList\n    Append I to a new array\n    Remove I from the arraylist\n</code></pre>\n'}, {'answer_id': 44297, 'author': 'Eric', 'author_id': 4540, 'author_profile': 'https://Stackoverflow.com/users/4540', 'pm_score': 2, 'selected': False, 'text': "<p>What you need to do is determine the ordering of B and then apply that ordering to A.  One way to accomplish this is to undo the ordering of B and keep track of what happens along the way.  Then you can do the reverse to A.</p>\n\n<p>Here's some sketchy C# (sorry, I haven't actually run this)...</p>\n\n<p>Take a copy of B:</p>\n\n<pre><code>List&lt;int&gt; B2 = new List&lt;int&gt;(B);\n</code></pre>\n\n<p>Now sort it, using a sort function that records the swaps:</p>\n\n<pre><code>List&lt;KeyValuePair&lt;int,int&gt;&gt; swaps = new List&lt;KeyValuePair&lt;int,int&gt;&gt;();\nB2.Sort( delegate( int x, int y ) {\n   if( x&lt;y ) return -1;\n   if( x==y ) return 0;\n   // x and y must be transposed, so assume they will be:\n   swaps.Add( new KeyValuePair&lt;int,int&gt;(x,y) );\n   return 1;\n});\n</code></pre>\n\n<p>Now apply the swaps, in reverse order, to A:</p>\n\n<pre><code>swaps.Reverse();\nforeach( KeyValuePair&lt;int,int&gt; x in swaps )\n{\n   int t = A[x.key];\n   A[x.key] = A[x.value];\n   A[x.value] = t;\n}\n</code></pre>\n\n<p>Depending how the built-in sort algorithm works, you might need to roll your own.  Something nondestructive like a merge sort should give you the correct results.</p>\n"}, {'answer_id': 674095, 'author': 'Jeremy', 'author_id': 67108, 'author_profile': 'https://Stackoverflow.com/users/67108', 'pm_score': 1, 'selected': False, 'text': "<p>Could the issue be resolved using a Dictionary so the elements have a relationship that isn't predicated on sort order at all?</p>\n"}, {'answer_id': 4269553, 'author': 'Yuri Astrakhan', 'author_id': 177275, 'author_profile': 'https://Stackoverflow.com/users/177275', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s my implementation of the comparer (uses LINQ, but can be easily adapted to older .net versions). You can use it for any sorting algorithms such as Array.Sort, Enumerable.OrderBy, List.Sort, etc.</p>\n\n<pre><code>var data = new[] { 1, 2, 3, 4, 5 };\nvar customOrder = new[] { 2, 1 };\nArray.Sort(data, new CustomOrderComparer&lt;int&gt;(customOrder));\nforeach (var v in data)\n    Console.Write("{0},", v);\n</code></pre>\n\n<p>The result is <code>2,1,3,4,5,</code> - any items not listed in the customOrder are placed at the end in the default for the given type (unless a fallback comparator is given)</p>\n\n<pre><code>public class CustomOrderComparer&lt;TValue&gt; : IComparer&lt;TValue&gt;\n{\n    private readonly IComparer&lt;TValue&gt; _fallbackComparer;\n    private const int UseDictionaryWhenBigger = 64; // todo - adjust\n\n    private readonly IList&lt;TValue&gt; _customOrder;\n    private readonly Dictionary&lt;TValue, uint&gt; _customOrderDict;\n\n    public CustomOrderComparer(IList&lt;TValue&gt; customOrder, IComparer&lt;TValue&gt; fallbackComparer = null)\n    {\n        if (customOrder == null) throw new ArgumentNullException("customOrder");\n\n        _fallbackComparer = fallbackComparer ?? Comparer&lt;TValue&gt;.Default;\n\n        if (UseDictionaryWhenBigger &lt; customOrder.Count)\n        {\n            _customOrderDict = new Dictionary&lt;TValue, uint&gt;(customOrder.Count);\n            for (int i = 0; i &lt; customOrder.Count; i++)\n                _customOrderDict.Add(customOrder[i], (uint) i);\n        }\n        else\n            _customOrder = customOrder;\n    }\n\n    #region IComparer&lt;TValue&gt; Members\n\n    public int Compare(TValue x, TValue y)\n    {\n        uint indX, indY;\n        if (_customOrderDict != null)\n        {\n            if (!_customOrderDict.TryGetValue(x, out indX)) indX = uint.MaxValue;\n            if (!_customOrderDict.TryGetValue(y, out indY)) indY = uint.MaxValue;\n        }\n        else\n        {\n            // (uint)-1 == uint.MaxValue\n            indX = (uint) _customOrder.IndexOf(x);\n            indY = (uint) _customOrder.IndexOf(y);\n        }\n\n        if (indX == uint.MaxValue &amp;&amp; indY == uint.MaxValue)\n            return _fallbackComparer.Compare(x, y);\n\n        return indX.CompareTo(indY);\n    }\n\n    #endregion\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/44270', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4541/']