locked
Shuffling while intializing a Observable Collection

    Question

  • Hi,

    I want to shuffle a content in ObservableCollection,

     public static ObservableCollection<whatsnewCompleteData> whatsnewCompleteList = new ObservableCollection<whatsnewCompleteData>();
    
     for (int i = 0; i < whatsnewfeed.feed.entry.Count; i++)
                {              
                        whatsnewCompleteList.Add(new whatsnewCompleteData(i, content[i]);
    
                }
    
    If it is normal list I can easily perform a shuffle! but observablecollection is directly bound to UI. Only thing I can do is using a random number each time while initializing.

    So here for the range of, 

    0 < whatsnewfeed.feed.entry.Count

    How do I generate a random number and that covers all values from 0 to whatsnewfeed.feed.entry.count?


    Sivakumar

    Monday, March 3, 2014 6:52 PM

Answers

  • Do something like this:

    void ShuffleObservable<T>(ObservableCollection<T> target)
    {
         ObservableCollection<T> intermediate = new ObservableCollection<T>();
         foreach(T item in target)
             Intermediate.Add(t);
         Target.Clear();
    
         Random R = new Random();
         while(intermediate.Count>0)
         {
              int index = R.Next(0, intermediate.Length-1);
              target.Add(intermediate[index]);
              intermediate.RemoveAt(index);
         }     
    }

    Then, call ShuffleObservable<MyItemType>(list), where MyItemType is the type of object stored in your observable collection.

    This will work well with small collections.  If you have millions of items, it will take a long time.

    I also haven't compiled it...  Ceck the Random.Next function- you may not have to subtract 1 from intermediate.Length IF .Next returns numbers between the two parameters.


    Darin R.

    Wednesday, March 5, 2014 11:48 PM

All replies

  • Hi,

    This is how you can generate a random number:

    Random random = new Random();
    int randomNumber = random.Next(0, whatsnewfeed.feed.entry.Count);

    (whatsnewfeed.feed.entry.Count not included)

    Monday, March 3, 2014 7:04 PM
  • But I may get repetitions in this way of approach! 

    Sivakumar

    Tuesday, March 4, 2014 3:25 PM
  • Do something like this:

    void ShuffleObservable<T>(ObservableCollection<T> target)
    {
         ObservableCollection<T> intermediate = new ObservableCollection<T>();
         foreach(T item in target)
             Intermediate.Add(t);
         Target.Clear();
    
         Random R = new Random();
         while(intermediate.Count>0)
         {
              int index = R.Next(0, intermediate.Length-1);
              target.Add(intermediate[index]);
              intermediate.RemoveAt(index);
         }     
    }

    Then, call ShuffleObservable<MyItemType>(list), where MyItemType is the type of object stored in your observable collection.

    This will work well with small collections.  If you have millions of items, it will take a long time.

    I also haven't compiled it...  Ceck the Random.Next function- you may not have to subtract 1 from intermediate.Length IF .Next returns numbers between the two parameters.


    Darin R.

    Wednesday, March 5, 2014 11:48 PM