Reputation: 2518
I'm using this code to sort an Array
Array.Sort(numsFoundedCount, numsFoundedSorted);
Which it works but when 2 items of numsFoundedSorted have the same numsFoundedCount value i want them (the numsFoundedSorted items) to be sorted from min to max.
int[] numsFoundedCount = new int[80];
int[] numsFoundedSorted = new int[80];
numsFoundedSorted inludes an integer and numsFoundedCount represent how many times this integer has appear. so i want to sort numsFoundedSorted from minumum to maximum according to numsFoundedCount.
I want both of the arrays to be sorted like in Array.Sort For example:
numsFoundedSorted {5,7,6,8}
numsFoundedCount {3,2,2,1}
After sort must be:
numsFoundedSorted {8,6,7,5}
numsFoundedCount {1,2,2,3}
Upvotes: 0
Views: 151
Reputation: 1137
If i understood right, this is what you want
var numsFoundedSorted = numsFoundedSorted
.Select((item, idx) => new { Index = idx, Value = item })
.OrderBy(tuple => numsFoundedCount[tuple.Index])
.ThenBy(tuple => tuple.Value)
.Select(tuple => tuple.Value)
.ToArray();
Array.Sort(numsFoundedCount);
There is no options to do it with Array.Sort
And if you are already using C# 7 features you can replace new { Index = idx, Value = item }
with (Index: idx, Value: item)
Upvotes: 2