jeudi 22 juin 2017

Randomize a list but with range

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.




Aucun commentaire:

Enregistrer un commentaire