c# list how to insert a new value in between two values

后端 未结 5 1293
無奈伤痛
無奈伤痛 2020-12-16 09:37

so i have a list where i need to add new values constantly but when i do i need to increment it and insert it in between two values.

List initiali         


        
5条回答
  •  甜味超标
    2020-12-16 10:09

    Another approach, if there is a computationally viable way of sorting the elements, is:

    list.Insert(num);
    // ...
    list.Insert(otherNum);
    
    // Sorting function. Let's sort by absolute value
    list.Sort((x, y) => return Math.Abs(x) - Math.Abs(y));
    

提交回复
热议问题