简体   繁体   中英

Sort Array based on an bother array from minimum to maximum value

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}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM