开发者

How can I shuffle my list of strings? [duplicate]

This question already has answers here: 开发者_开发问答 Closed 11 years ago.

Possible Duplicate:

Randomize a List<T> in C#

I thought I had my code working but now it seems not. Here's what I have:

public class NoteDetail
{
    public NoteDetail()
    {
        _noteDetails = new List<string>();
    }
    public IList<string> NoteDetails { get { return _noteDetails; } }
    private readonly List<string> _noteDetails;
}

I populate my details like this:

        var noteDetail = new NoteDetail ();
        noteDetail.NoteDetails.Add("aaa");
        noteDetail.NoteDetails.Add("bbb");
        noteDetail.NoteDetails.Add("ccc");

Now I want to shuffle so I used this routine:

    public static void ShuffleGenericList<T>(IList<T> list)
    {
        //generate a Random instance
        var rnd = new Random();
        //get the count of items in the list
        var i = list.Count();
        //do we have a reference type or a value type
        T val = default(T);

        //we will loop through the list backwards
        while (i >= 1)
        {
            //decrement our counter
            i--;
            //grab the next random item from the list
            var nextIndex = rnd.Next(i, list.Count());
            val = list[nextIndex];
            //start swapping values
            list[nextIndex] = list[i];
            list[i] = val;
        }
    }

My problem is that I am not sure how to do the shuffle. I have tried the following but it gives:

Error 237 Argument 1: cannot convert from 'System.Collections.Generic.IList' to 'System.Collections.Generic.IList<.Storage.Models.NoteDetail>'

Sort.ShuffleGenericList<NoteDetail>(noteDetail.NoteDetails);

Can anyone see what I am doing wrong. It all looks okay to me and I can't see why I should get this error :-(


You should change this:

Sort.ShuffleGenericList<NoteDetail>(noteDetail.NoteDetails);

To:

Sort.ShuffleGenericList<string>(noteDetail.NoteDetails);

Because noteDetail.NoteDetails is a List<string>, not a List<NoteDetail>.


You are using the wrong type to parametrize your generic method, do this instead:

Sort.ShuffleGenericList(noteDetail.NoteDetails);

or more explicit (but unneccessary):

Sort.ShuffleGenericList<string>(noteDetail.NoteDetails);

You were passing NoteDetail as type, rather than string - that won't work.


I took your code and threw it into VS. The below execustes okay with a few small modifications:

using System;
using System.Collections.Generic;
using System.Linq;

namespace MsgBaseSerializeationTest
{
    class StackOverflow
    {
        public void Test()
        {
            var noteDetail = new NoteDetail<string>();
            noteDetail.NoteDetails.Add("aaa");
            noteDetail.NoteDetails.Add("bbb");
            noteDetail.NoteDetails.Add("ccc");

            NoteDetail<string>.ShuffleGenericList(noteDetail);
        }

    }


    public class NoteDetail<T> : List<T>
    {
        public NoteDetail()
        {
            _noteDetails = new List<string>();
        }
        public IList<string> NoteDetails { get { return _noteDetails; } }
        private readonly List<string> _noteDetails;

        public static void ShuffleGenericList(IList<T> list)
        {
            //generate a Random instance
            var rnd = new Random();
            //get the count of items in the list
            var i = list.Count();
            //do we have a reference type or a value type
            T val = default(T);

            //we will loop through the list backwards
            while (i >= 1) {
                //decrement our counter
                i--;
                //grab the next random item from the list
                var nextIndex = rnd.Next(i, list.Count());
                val = list[nextIndex];
                //start swapping values
                list[nextIndex] = list[i];
                list[i] = val;
            }
        }


    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜