0

I have a dictionary that maps SortedSet of transactions with transaction status - Dictionary<TransactionStatus, SortedSet<Transaction>> _transactionsByStatus = new Dictionary<TransactionStatus,SortedSet<Transaction>>();

I know if I want to intialize a SortedSet with custom comparer I can do - SortedSet<Transaction> _transactions = new SortedSet<Transaction>(new CustomComparer());

How I can do the same with SortedSet that is used as Values for _transactionsByStatus dictionary ?

NUM1NEX
  • 1
  • 1
  • 3
  • The Transaction class need to impliement the IComparable : https://learn.microsoft.com/en-us/dotnet/api/system.icomparable-1?view=net-6.0&force_isolation=true – jdweng Sep 20 '22 at 19:57
  • @jdweng like this - `public class Transaction : IComparable` and implementation - `public int CompareTo(Transaction? other) var comparer = this.Amount.CompareTo(other.Amount); return comparer;` If so how I can now make the SortedSet to be sorted in descending order and in case of equal `amount` orderd by ascending by `id`? – NUM1NEX Sep 20 '22 at 20:35
  • list.GroupBy(x => x.id).SelectMany(x => x.OrderByDescending(y => y.Amount)).ToList(). A GroupBy greats a two dimensional array list>. The SelectMany takes the two dimensional array and make one dimension. – jdweng Sep 20 '22 at 20:48
  • I was wondering if I can get away without using LINQ expressions, thats the reason why I decided to use SortedSet as my data strucuture I wanted the `SortedSet` to be already sorted in descending order by amount then ascending by id on the access time `_transactionsByStatus[status].Value` , but I guess i will just switch from SortedSet to List and just have the ordering be handled directly in the method that Im using to return transactions – NUM1NEX Sep 20 '22 at 21:10

0 Answers0