question

donovan-3047 avatar image
0 Votes"
donovan-3047 asked donovan-3047 edited

How to swap and sort item in List at the same time?

Let's say there is a list where we randomly place items:

 List<string> original = new List<string>() { A,B,C,D,E,F,G,H,I,J };
 Random random = new Random();
 random_list = original.OrderBy(x => random.Next()).ToList();

That's not the issue. Say that after shuffling:

 List<string> random_list = { E,J,A,D,H,F,C,I,G,B };

I would like to sort each random_list item randomly. So pick a random number: say 5, swap item 5 in the random_list to the "original" position, and swap it with the value that is already there. Then 5 can't be swapped anymore, because it's at the correct place. Swap then 2, then 9 etc... until the random_list is back to its original state.

I may overthink the issue. How to achieve this?








dotnet-csharpwindows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@donovan-3047 . Is there an update to your question? Did my answer solve your problem? If so, you could accept it as an answer. It is helpful for community members with similar issues.

0 Votes 0 ·

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered donovan-3047 edited

List<T> doesn't trigger any notifications, so the UI cannot detect when the contents of the list have changed.
I replace List<string> with ObservableCollection to update list data.
MainWindow.xaml:

   <StackPanel x:Name="sp">
         <TabControl x:Name="txtoriginal" ItemsSource="{Binding original, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}"/>
         <TabControl x:Name="txtrandom_list" ItemsSource="{Binding random_list, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}"/>
         <TextBox x:Name="input" Background="AliceBlue" Height="40" Margin="5"/>
         <Button x:Name="swap" Content="swap" Margin="5" Click="swap_Click" />
     </StackPanel>

MainWindow.xaml.cs:

 using System;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Linq;
 using System.Windows;
    
 namespace SwapAndSort
 {
   public partial class MainWindow : Window,INotifyPropertyChanged
   {
     public ObservableCollection<string> original { get; set; }
     public ObservableCollection<string> random_list { get; set; }
     public ObservableCollection<string> Random_list
     {
       get
       {
         return random_list;
       }
       set
       {
         random_list = value;
         PropertyChanged(this, new PropertyChangedEventArgs("Random_list"));
       }
     }
     public MainWindow()
     {
       InitializeComponent();
       original = new ObservableCollection<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
       Random random = new Random();
       random_list = new ObservableCollection<string>(original.OrderBy(x => random.Next()).ToList());
       DataContext=this;
     }
     public event PropertyChangedEventHandler PropertyChanged;
     private void swap_Click(object sender, RoutedEventArgs e)
     {
       int index = Int32.Parse( input.Text);
       string str = original[index];
       int index1 = random_list.IndexOf(str);
       random_list = (ObservableCollection<string>)random_list.Swap(index, index1);
     }
   }
   public static class C
   {
     public static ObservableCollection<T> Swap<T>(this ObservableCollection<T> list, int indexA, int indexB)
     {
       T tmp = list[indexA];
       list[indexA] = list[indexB];
       list[indexB] = tmp;
       return list;
     }
   }
 }


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Perfect! And do you know how to shuffle the Original and make sure there is NO one at the same position?

0 Votes 0 ·