簡體   English   中英

根據從最小到最大值的麻煩數組對數組進行排序

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

我正在使用此代碼對數組進行排序

Array.Sort(numsFoundedCount, numsFoundedSorted);

它的工作原理,但是當numsFoundedSorted的2個項目具有相同的numsFoundedCount值時,我希望它們(最小值和最大值)進行排序。

int[] numsFoundedCount = new int[80];
int[] numsFoundedSorted = new int[80];

numsFoundedSorted包含一個整數,並且numsFoundedCount表示此整數出現了多少次。 所以我想根據numsFoundedCount從最小到最大對numsFoundedSorted進行排序。

我希望兩個數組都像在Array.Sort中那樣排序:

numsFoundedSorted {5,7,6,8}
numsFoundedCount {3,2,2,1}

排序后必須是:

numsFoundedSorted {8,6,7,5}
numsFoundedCount {1,2,2,3}

如果我理解正確,這就是您想要的

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);

沒有選擇與Array.Sort一起Array.Sort

並且,如果您已經在使用C#7功能,則可以將new { Index = idx, Value = item }替換為(Index: idx, Value: item)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM