sorting of an array based on the closed sequence

if you have a constant list to OrderBy

my approach would be

var orderOfPreference = new List<string> { "USD", "EUR", "GBP", "CNY" };
var orderedResults = derivativeTrades.OrderBy(d => {
                var index = orderOfPreference.IndexOf(d.SettlementCurrencyCode);
                return index == -1 ? int.MaxValue : index;
            }).ThenBy(x => x.SettlementCurrencyCode);

the return index == -1 ? int.MaxValue : index will place the not included in orderOfPreference outside of the index position of the list. ThenBy ensures it will arrange everything after the orderOfPreference alphabetically. you can change int.MaxValue by the length of data

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top