See*_*umi 0 c# shuffle sortedlist
我是C#的新手,并试图创建一个MP3播放器.我正在使用sortedList作为播放列表(歌曲名称为Key,文件路径为Value),但我不确定如何随机化shuffle的列表顺序.
我试过这种方法,但它没有提出任何新订单,它从列表中删除了一首歌,嘿.
private SortedList Shuffle(SortedList oldSongList)
{
SortedList newSongList = new SortedList();
Random r = new Random();
int n = oldSongList.Count;
while (n > 1)
{
int rand = r.Next(n);
newSongList.Add(oldSongList.GetKey(rand), oldSongList.GetByIndex(rand));
oldSongList.RemoveAt(rand);
oldSongList.TrimToSize();
n--;
}
return newSongList;
}
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?