c# - How to efficiently get highest & lowest values from a List<double?>, and then modify them? -


I need to get a list of doubles. If the amount is> 100, then I should subtract from the highest number unless it is = 100. If the amount is & lt; 100, I have to increase the number to the least until it = 100 not I can do this in the list, while assigning the value of the placeholder variable and following higher or lower examinations, but I am thinking that any guru can suggest from there super cool & amp; Is there an efficient way to do this? The code given below is basically what I am trying to achieve:

  var splitValues ​​= new list & lt; Double? & Gt; (); SplitValues.Add (Math.Round (assetSplit.EquityTypeSplit (?? 0)); SplitValues.Add (math.road (assetSplit.PropertyTypeSplit () ?? 0)); SplitValues.Add (Math.Round (assetSplit.FixedInterestTypeSplit () ?? 0)); SplitValues.Add (Math.Round (assetSplit.CashTypeSplit () ?? 0)); Var listSum = splitValues.Sum (partition = & gt; split.Value); If (listSum! = 100) {if (listSum> 100) {// How to get the highest value and list up to 1 list = = 100 100 / then re-assigned to the split price list? Highest = // ?? } Else {// How to get the lowest where the value is & gt; Increase in the list of 0, and 1 up to == 100 // then reassign it into the split price list? Var lowest = // ?? }}  

Update: The list must be in the same order as items are added.

I want to do something like this:

  var splitValues = New list & lt; Double? & Gt; (); SplitValues.Add (Math.Round (assetSplit.EquityTypeSplit (?? 0)); SplitValues.Add (math.road (assetSplit.PropertyTypeSplit () ?? 0)); SplitValues.Add (Math.Round (assetSplit.FixedInterestTypeSplit () ?? 0)); SplitValues.Add (Math.Round (assetSplit.CashTypeSplit () ?? 0)); Var listSum = splitValues.Sum (partition = & gt; split.Value); While (listSum! = 100) {var value = listSum> 100? SplitValues.Max (): splitValues.Min (); Var idx = splitValues.IndexOf (value); SplitValues.RemoveAt (IDX); SplitValues.Instert (idx, value + (list list> 100? -1: 1)); ListSum = splitValues.Sum (partition = & gt; split.Value); }  Note:  This solution will work for any number in the list. 


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -