0

I have a List with 72 items in it. The program is kinda like Tinder where a picture and some text is shown.

I want this List to be randomized but not the first "card" and the last "card" eg. item no. 1 & item no. 72 those must remain as the first and last card, the remaining 70 items will be sorted in a random order.

Here is my snippet of code where I define the list

public class MainPageViewModel : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   List<CardStackView.Item> items = new List<CardStackView.Item>();
   public List<CardStackView.Item> ItemsList
   {
      get { return items; }
      set { if (items == value) { return; } items = value; OnPropertyChanged(); }
   }

   protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
   {
      PropertyChangedEventHandler handler = PropertyChanged;
      if(handler != null)
      {
         handler(this, new PropertyChangedEventArgs(propertyName));
      }
   }

   protected virtual void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
   {
      field = value;
      PropertyChangedEventHandler handler = PropertyChanged;
      if(handler != null)
      {
         handler(this, new PropertyChangedEventArgs(propertyName));
      }
   }

   public MainPageViewModel()
   {
      items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 1 });
      items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 2 });
      items.Add ........
      items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 72 });
   }
}

Can I do it while they all are in one list, or should I make a multidimensional array. Where index 1 are item no. 1, index 2 are Randomized List & index 3 are item no. 72. If this is a proper solution, how would i go about showing them on my cards.

I have been looking at questions like these Randomize a List<T> & Best way to randomize an array with .NET, but i with no success.

TheGejr
  • 47
  • 3
  • 10
  • 1
    You are making the list so why not make a list of the items you want shuffled, then shuffle them based on the answers to the questions you've already linked to, and then when you're done shuffling add your static Item#1 and Item#72 into the appropriate places? – Mashton Jun 22 '17 at 09:39
  • @Mashton oh yeah why didn't i think of that *facepalm*, i will try that :) – TheGejr Jun 22 '17 at 09:42

2 Answers2

1

It's very easy to adapt a standard random shuffle algorithm to accept a starting index and a count:

public static void Shuffle<T>(IList<T> array, Random rng, int first, int count)
{
    for (int n = count; n > 1;)
    {
        int k = rng.Next(n);
        --n;
        T temp = array[n+first];
        array[n + first] = array[k + first];
        array[k + first] = temp;
    }
}

Then if you want to shuffle all but the first and the last items:

Shuffle(items, new Random(), 1, items.Count-2);
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

Try this:

private Random random = new Random();

public MainPageViewModel()
{
    /* Populate `items` */

    items =
        items
            .Take(1)
            .Concat(items.Skip(1).Take(items.Count() - 2).OrderBy(x => random.Next()))
            .Concat(items.Skip(items.Count() - 1))
            .ToList();
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172