How to sort ComboBox without making a sort code.

VoyTec 671 Reputation points
2023-03-12T21:34:25.1266667+00:00

I am wondering if there is a simple call for sorting ComboBox, without advanced code with fors, x++ etc...

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,411 questions
{count} votes

Accepted answer
  1. Roy Li - MSFT 34,281 Reputation points Microsoft External Staff
    2023-03-13T06:54:39.42+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Currently, the ComboBox control doesn't contain a built-in method to sort its items. If you need to sort the items of the ComboBox, you will need to sort the item source of the ComboBox instead. The simplest way is to sort the source list with LINQ. Use the orderby command of LINQ sentence to change the source list.

    Here is a sample I made and you could take look at it.

    Xaml:

    
      <Grid>
            <ComboBox x:Name="FontsCombo" Header="Fonts" Height="88" Width="296"  DisplayMemberPath="Id"/>
        </Grid>
    
    

    Code-behind:

      public sealed partial class MainPage : Page
        {
            public List<TestModel> source { get; set; }
            public MainPage()
            {
                this.InitializeComponent();
                source = new List<TestModel>();
                this.Loaded += MainPage_Loaded;
                
            }
    
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    Random random = new Random();
                    int RandomNumber = random.Next(0, 100);
                    TestModel model = new TestModel { Id = RandomNumber};
                    source.Add(model);
                }
                // sort the source list by ID
                var newSource= from item in source
                               orderby item.Id
                               select item;
    
                FontsCombo.ItemsSource = newSource;
            }
        }
    
        public class TestModel 
        {
            public int Id { get; set; }
        }
    

    I generated 10 items with random numbers. Then I use the LINQ to get the newSource as the source of the ComboBox.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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 additional answer

Sort by: Most helpful
  1. VoyTec 671 Reputation points
    2023-03-13T19:29:04.0766667+00:00

    I made a workaround to save data in right position:

    int count_companies = int.Parse(data_read[0]) + 1;
                            file_save = count_companies.ToString();
    
                            int compare_1;
                            int compare_2;
    
                            for (int x = 0; x < int.Parse(data_read[0]); x++)
                            {
                                if (x > 0)
                                {
                                    compare_1 = company_name_TB.Text.CompareTo(data_read[x * 4 - 3]);
    
                                    compare_2 = company_name_TB.Text.CompareTo(data_read[(x * 4) + 1]);
                                    if ((compare_1 > 0) && (compare_2 < 0))
                                    {
                                        file_save = file_save + "\n" + company_name_TB.Text;
                                        file_save = file_save + "\n" + company_text_TB.Text;
                                        file_save = file_save + "\n" + company_nip_TB.Text;
                                        file_save = file_save + "\n" + company_adress_TB.Text;
                                    }
                                }
    
    
                                data_read.Add(data_read[x * 4 + 1]);
                                data_read.Add(data_read[x * 4 + 2]);
                                data_read.Add(data_read[x * 4 + 3]);
                                data_read.Add(data_read[x * 4 + 4]);
    
                                file_save = file_save + "\n" + data_read[x * 4 + 1];
                                file_save = file_save + "\n" + data_read[x * 4 + 2];
                                file_save = file_save + "\n" + data_read[x * 4 + 3];
                                file_save = file_save + "\n" + data_read[x * 4 + 4];
                            }
    
                            await FileIO.WriteTextAsync(file_firms_load, file_save, Windows.Storage.Streams.UnicodeEncoding.Utf8);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.