locked
Sort words RRS feed

  • Question

  • I have a FlowDocument with several lines of text:

    This is my text1 first WORD first text1 to the right.

    This is my text2 second WORD second text2 to the right.

    This is my text3 third WORD third text3 to the right.

    This is my text4 fourth WORD fourth text4 to the right.

    ...

    The "WORD" word is the same in all the lines.

    What would be the best approach to sort in all the lines say, the first and second word to the leff of "WORD" and the first word to the right of "WORD"?

    Wednesday, March 5, 2014 12:05 PM

All replies

  • Hi,

    I am not exactly sure your mean. I supposed that there is a ListBox control in the FlowDocument control. You want to sort the content in the ListBox or similar.

    I created a sample on my end about how to sort a GridView column when a Header is clicked.

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    
        GridViewColumnHeader _lastHeaderClicked = null;
        ListSortDirection _lastDirection = ListSortDirection.Ascending;
    
        void GridViewColumnHeaderClickedHandler(object sender,
                                                RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked =
                  e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;
    
            if (headerClicked != null)
            {
                if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                {
                    if (headerClicked != _lastHeaderClicked)
                    {
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (_lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }
    
                    string header = headerClicked.Column.Header as string;
                    Sort(header, direction);
    
                    if (direction == ListSortDirection.Ascending)
                    {
                        headerClicked.Column.HeaderTemplate =
                          Resources["HeaderTemplateArrowUp"] as DataTemplate;
                    }
                    else
                    {
                        headerClicked.Column.HeaderTemplate =
                          Resources["HeaderTemplateArrowDown"] as DataTemplate;
                    }
    
                    // Remove arrow from previously sorted header 
                    if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
                    {
                        _lastHeaderClicked.Column.HeaderTemplate = null;
                    }
    
                    _lastHeaderClicked = headerClicked;
                    _lastDirection = direction;
                }
            }
        }
    }
    
    private void Sort(string sortBy, ListSortDirection direction)
    {
        ICollectionView dataView =
          CollectionViewSource.GetDefaultView(lv.ItemsSource);
    
        dataView.SortDescriptions.Clear();
        SortDescription sd = new SortDescription(sortBy, direction);
        dataView.SortDescriptions.Add(sd);
        dataView.Refresh();
    }

    For more information: See How to: Sort a GridView Column When a Header Is Clicked. Here is a thread about sort function in DataGrid/ListBox and etc. See: Best way to make WPF ListView/GridView sort on column-header clicking

    If I misunderstood or my points are incorrect, please feel free to let me know. And provide more information about this issue, maybe a screenshot would be better. J

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 6, 2014 6:29 AM
  • Hi Jimmy,

    Sorry for the poor description of the problem. This a specific requirement in which I can search in a text for the different occurrences of a selected word ("features", in the sample), with its contexts (e.g., 35 characters to left and 35 to the right). This allows to investigate the different occurrences of that word and in the end, will show in which context words the selected word occurs the most. I'm sending two images as you recommended: one with a sample before the sort and the other, after the sort.

    So, what I'm looking to achieve is how to sort the words in the context to the left and to the right of the concordance word and display the result in a FlowDocument. I hope it’s a bit clear now.

    Sample before sort

    Sample after sort


    Thursday, March 6, 2014 11:58 AM
  • Hi Manuel,

    Maybe I understand your mean. Firstly, let me description this requirement. You want to sort the word before “features” or other words, also sort the word behind “features” or other words. And order the sort rule.

    We can achieve this goal. I supposed that the five sentences are five paragraph. We can get each sentence and store it in string object or similar. And then get the words before and behind “features”. Sorting the words by the specify rule. After all, set the sentences according to the order.

    Hope this helps.

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 7, 2014 7:44 AM
  • Hi Jimmy,

    What I'm looking for is a technical recommendation within WPF/C# and not a different verbalization of my initial statement. I know about the several Framework objects available the question is, which one(s) to use and how to best achieve my goal. Thank you for your answer anyway.

    Friday, March 7, 2014 10:47 AM
  • Hi Manuel,

    You should start your program first. There are many different sort function in C#. Here I research some references about this issue.

    http://www.codeproject.com/Articles/80546/Comparison-Sorting-Algorithms-in-C-Explained

    http://www.codeproject.com/Articles/6033/Sorting-Algorithms-In-C

    Monday, March 10, 2014 7:08 AM
  • Hi James,

    Again, I'm not looking for advice on how to "start a program" (whatever you mean by that...). But, if you mean, "do it by yourself...", I've already realized it by the replies I got to my question, and I'm working on that.

    Thank you for your references.

    Monday, March 10, 2014 9:58 AM
  • Hi Manuel,

    Sorry for the late reply. J

    >>What I'm looking for is a technical recommendation within WPF/C# and not a different verbalization of my initial statement. <<

    I am not sure I have understood this. As far as I know, there are no framework can achieve this goal directly. We should use the basic C# language to get each statement and sort them.

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 11, 2014 9:18 AM