0

I am trying to arrange and output the elements of the list in specific order provided.

The original list is as follows:

var outcome = Result.ToArray() 

The Result returns elements in format based on class signature as below:

class ResultOutcome {
public string id;
public string val;
}

so the outcome will have elements such as :

Result.ToArray()[0] = {id : "1", val: "100"}
Result.ToArray()[1] = {id : "2", val: "50"}
Result.ToArray()[2] = {id : "3", val: "75"}
Result.ToArray()[3] = {id : "4", val: "98"}
Result.ToArray()[4] = {id : "5", val: "100"}
etc.

I would like to arrange the outcome in the order where it is listed with ids in the order of

int  requiredOrder [] = new int[5] {3,5,2,1,4}

so the outcome will be

Result.ToArray()[0] = {id : "3", val: "75"}
Result.ToArray()[1] = {id : "5", val: "100"}
Result.ToArray()[2] = {id : "2", val: "50"}
Result.ToArray()[3] = {id : "1", val: "100"}
Result.ToArray()[4] = {id : "4", val: "98"}

I have tried following given as per Ordering list by a specific order


var outcome =  Result.ToArray();

int[] sequence = new int[5] { 3,4,1,2,5 };

Dictionary<int, int> sequence IndexedMap = Enumerable.Range(0, sequence .Length - 1).ToDictionary(r => sequence [r], r => r);

However, I am not sure how can I filter after it
  • 2
    Does this answer your question? [Sort a list from another list IDs](https://stackoverflow.com/questions/15275269/sort-a-list-from-another-list-ids) – Andrew Morton Aug 15 '23 at 19:37

2 Answers2

0

If your statement exactly as it (the required order is expressed by numbers from the set of sequential integers starting from N):

public class ResultOutcomeComparer : IComparer {
    int IComparer.Compare(Object x, Object y) {
        return ((new CaseInsensitiveComparer())
            .Compare(((ResultOutcome)x).id, ((ResultOutcome)y).id));
    }
}
...
Array.Sort(ResultToArray, new ResultOutcomeComparer());
var tmp = new int[requiredOrder.Length];
for (int i = 0; i < requiredOrder.Length; i++) tmp[requiredOrder[i] - N] = i; 
Array.Sort(tmp, ResultToArray);

Demo, N = 1:

using System;
using System.Collections;
class ResultOutcome { public string id, val; }
public class ResultOutcomeComparer : IComparer {
    int IComparer.Compare(Object x, Object y) {
        return ((new CaseInsensitiveComparer())
            .Compare(((ResultOutcome)x).id, ((ResultOutcome)y).id));
    }
}
class SortByOrder {
    static void Main() {
        const int ML = 5;
        var ResultToArray = new ResultOutcome[ML] {
            new ResultOutcome() { id = "1", val = "100" }
            , new ResultOutcome() { id = "3", val = "75" }
            , new ResultOutcome() { id = "4", val = "98" }
            , new ResultOutcome() { id = "2", val = "50" }
            , new ResultOutcome() { id = "5", val = "100" }
            };
        var requiredOrder = new int[ML] { 3, 5, 2, 1, 4 };

        Array.Sort(ResultToArray, new ResultOutcomeComparer());
        var tmp = new int[ML];
        for (int i = 0; i < ML; i++) tmp[requiredOrder[i] - 1] = i; 
        Array.Sort(tmp, ResultToArray);

        for (int i = 0; i < ML; i++)
            Console.WriteLine("{0} {1}", ResultToArray[i].id, ResultToArray[i].val);
    }
}
rotabor
  • 561
  • 2
  • 10
0

Others already suggested ways for sorting the way you asked, so I'll suggest slightly changing your approach as something to consider as well. Instead of using a different array (requiredOrder in your example), try adding a sortOrder field to ResultOutcome class:

class ResultOutcome {
    public string id;
    public string val;
    public int sortOrder;
}

Set the sortOrder field value for every element in the array when you instantiate the array:

var Result = new List<ResultOutcome> {
    new ResultOutcome { id = "1", val = "100", sortOrder = 4 },
    new ResultOutcome { id = "2", val = "50", sortOrder = 3 },
    new ResultOutcome { id = "3", val = "75", sortOrder = 1 },
    new ResultOutcome { id = "4", val = "98", sortOrder = 5 },
    new ResultOutcome { id = "5", val = "100", sortOrder = 2 }
};

Sort the array by the sortOrder field:

var sortedArray = Result.OrderBy(element => element.sortOrder);