0

I have a table data with many columns, I need to sort it by multiple times. e.g. the columns contain Row#, Name, allowance, times...

1, Peter,   2500,  2
2, Steve,    500,  1
3, Peter,   2500,  1
4, Peter,   2600,  2
5, Peter,   2800,  2

1st Sort by Name ascending, the result is:

1, Peter,   2500,  2
3, Peter,   2500,  1
4, Peter,   2600,  2
5, Peter,   2800,  2
2, Steve,    500,  1

2nd Sort by allowance descending, the result is:

5, Peter,   2800,  2
4, Peter,   2600,  2
1, Peter,   2500,  2
3, Peter,   2500,  1
2, Steve,    500,  1

3rd Sort by times ascending, the result is:

5, Peter,   2800,  2
4, Peter,   2600,  2
3, Peter,   2500,  1
1, Peter,   2500,  2
2, Steve,    500,  1

Due to some reason, We used SortedList to do the first sort using IComparer to add item to SortedList to allow duplicated keys. After 1st sort by Name ascending, sortedList is:

["Peter"] | 1
["Peter"] | 3
["Peter"] | 4
["Peter"] | 5
["Steve"] | 2

The 1st sort is done, but how to do 2nd and 3rd... sort based on this 'Master' sortedList? My idea is that 2nd sort is happened within neighbors w/ the same Key e.g. "Peter". means only need to re-sort Row# 1,3,4,5 for 2nd Sort. But I can't think the best way.

qtg
  • 125
  • 1
  • 11
  • Please follow this link [Here](https://stackoverflow.com/questions/9107916/sorting-rows-in-a-data-table) – M.Y.Mnu Dec 28 '17 at 13:25
  • I have to use SortedList. The old version used SortedList for 1st sort. I need to add new features of multiple sort. – qtg Dec 28 '17 at 13:29
  • Code for your `IComparer` please ;) – zc246 Dec 28 '17 at 13:44
  • @zcui93 2nd Sort is based one 1st sort result, 3rd sort is based on 2nd sort...for 2nd sore, only sort Key of "Peter", for 3rd sort, only need to sort items of "2500". I have to compose 2nd SortedList and 3rd SortedList? – qtg Dec 28 '17 at 14:21
  • You mentioned you have one `IComparer` already, potentially you could just implement another one to achieve what you want (although it's not quite clear what exactly you want to achieve, e.g. you want 1 table or 3? you want user interaction or just need the result). Hence what's the `IComparer` code you already used could be useful to be listed in your question. – zc246 Dec 28 '17 at 14:25

1 Answers1

0

You can use LinQ on your Sorted list:

var Customsortedlist =  SortedList<T>.OrderBy(e => e.Name)
                                     .ThenByDescending(e => e.Allowance)
                                     .ThenBy(e => e.times );

Then you can convert it like this :

dataTable = Customsortedlist.AsDataView().ToTable();
KHL
  • 455
  • 2
  • 12
  • Linq is a good choice. But I an not sure the efficient if have 1 million records. BTW,how to add my own IComparer into Linq? Because the compare could be a Cell with different font/backcolor/...objects. – qtg Dec 28 '17 at 13:44
  • You can add your comparer like this : var Customsortedlist = SortedList.OrderBy(e => e.Name, new MyComparerClass())... – KHL Dec 28 '17 at 13:47
  • OK. I accept this answer because it is practical if we used Linq even though I didn't use Linq for this project. – qtg Jan 11 '18 at 11:22