This is a migrated thread and some comments may be shown as answers.

Sorting Numerical items

1 Answer 99 Views
ListView
This is a migrated thread and some comments may be shown as answers.
George C.
Top achievements
Rank 2
Iron
Veteran
George C. asked on 30 Mar 2020, 11:26 AM

Greetings,

I have a Radlistview, a Textbox, and a Button. User enters a decimal or non-decimal number in the Textbox (For example, 3.1) and clicks the button.

Here is the clicking event of the button :

' RadlistView sorting is enabled in loading event of my form.
 
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Radlistview1.Items.Add(Textbox1.Text)
Dim sort = New SortDescriptor("Text", ListSortDirection.Ascending)
Radlistview1.SortDescriptors.Add(sort)
End Sub

 

I did a test :

1- Adding the first item to RadlistView with textbox value set to 20.

2- Adding the Second item to RadlistView with textbox value set to 2.1.

 

As sorting direction is set to Ascending (also tested with descending), 2.1 should be placed as the first item because it is smaller than 20. But it doesn't work.

I appreciate any solution to this issue. Thanks

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 31 Mar 2020, 10:43 PM

Hello George,

If you want to sort numerical values in RadListView you should create a custom comparer that will compare decimal values entered from the TextBox.

To achieve this, create a custom class that implements the IComparer interface and introduce your own logic. Refer to the Custom Sorting documentation article for more information.

A sample example is shown below:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            this.radListView1.Items.Add(this.radTextBox1.Text);
            var sort = new SortDescriptor("Text", ListSortDirection.Ascending);
            this.radListView1.SortDescriptors.Add(sort);
            
            this.radListView1.ListViewElement.DataView.Comparer = new ListViewCustomComparer(this.radListView1.ListViewElement);
            this.radListView1.EnableSorting = true;
        }
        
    }

    public class ListViewCustomComparer : IComparer<ListViewDataItem>
    {
        RadListViewElement listViewElement;

        public ListViewCustomComparer(RadListViewElement listViewElement)
        {
            this.listViewElement = listViewElement;
        }

        public int Compare(ListViewDataItem x, ListViewDataItem y)
        {
            decimal row1Freight = decimal.Parse(x.Text);
            decimal row2Freight = decimal.Parse(y.Text);
            
            return row1Freight.CompareTo(row2Freight);
        }
    }

Note, that this is a demonstrative approach. Feel free to modify or extend it in a way that is most suitable for you.

I hope this helps. Should you have other questions do not hesitate to ask.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
ListView
Asked by
George C.
Top achievements
Rank 2
Iron
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or