0

This might be easy. I have few question. My listview items have a data like this

col/row Name age distance

        Flick 20 1.4
        Hamn  19 0.9
        John  19 1.0

And I want to sort the list view by checking the lowest distance. So Its gonna be like this:

col/row Name age distance

        Hamn  19 0.9
        John  19 1.0
        Flick 20 1.4

I am trying to use a listview control name Sorting. But it not working. My question is how I am gonna sort the data by checking item.subitems[2] and make my listview to be like the new one above.PS.Also my data have more than 3 data not just Hamn,John,Flick.

Thanks you very much.

2
  • check this answer : stackoverflow.com/questions/50550448/… Commented Jul 6, 2018 at 17:55
  • Been a long time since I have done it, but I think you can implement a custom comparer that will be called be the ListView when it is sorted, The comparer would compare the distance of your items. See the ListViewItemSorter property. Commented Jul 6, 2018 at 18:04

2 Answers 2

1
var orderedList = your_list.OrderBy(r => r.subitem);

And also you can use from your own sorting method (I think don't required in your case)

usage :

var orderedList = your_list.OrderBy(r => GetOrder(r.subitem));

sample method :

Public Static int GetOrder(string _arg)
{
    switch (_arg)
    {
        case 'Section One':
            return 1;
        case 'Section Two':
            return 2;
        case 'Section Three':
            return 3;
            .
            .
            .
        default:
            return int.MaxValue;
    }
}

-------------Edit : Added Example Project --------------------

Just Use from this code :

var list = List_unsort.OrderBy(x => x.number);

Example Project :

public static void Main()
    {

        Console.WriteLine("Start Work ...\r\n");

        Create_list();

        Sort_list();

        Console.WriteLine("\r\nFinish.");

    }

    private static List<custom_item> List_unsort = new List<custom_item>();

    private class custom_item
    {
        public int id { get; set; }

        public string name { get; set; }

        public double number { get; set; }
    }


    private static void Create_list()
    {
        Random rnd = new Random();
        Console.WriteLine("Unsort List : ");
        for (int i = 0; i < 10; i++)
        {
            custom_item item = new custom_item();
            item.name = i.ToString();
            item.id = i;

            double rnd_num = rnd.NextDouble();
            item.number = rnd_num;

            Console.WriteLine("ID : " + item.id + ", number : " + item.number);
            List_unsort.Add(item);
        }
        Console.WriteLine("\r\n--------------------\r\n");
    }

    private static void Sort_list()
    {
        var list = List_unsort.OrderBy(x => x.number);
        Console.WriteLine("Sort List : ");
        foreach (var item in list)
        {
            Console.WriteLine("ID : " + item.id + ", number : " + item.number);
        }
    }

and Output :

Start Work ...

Unsort List : ID : 0, number : 0.333175984366413

ID : 1, number : 0.886352685227223

ID : 2, number : 0.633657003582295

ID : 3, number : 0.319651832021611

ID : 4, number : 0.340439343983512

ID : 5, number : 0.476074807567557

ID : 6, number : 0.664470672451179

ID : 7, number : 0.14823883685667

ID : 8, number : 0.375242880254631

ID : 9, number : 0.186346486763259


Sort List :

ID : 7, number : 0.14823883685667

ID : 9, number : 0.186346486763259

ID : 3, number : 0.319651832021611

ID : 0, number : 0.333175984366413

ID : 4, number : 0.340439343983512

ID : 8, number : 0.375242880254631

ID : 5, number : 0.476074807567557

ID : 2, number : 0.633657003582295

ID : 6, number : 0.664470672451179

ID : 1, number : 0.886352685227223

Finish.

Link for Compile and test project :

http://rextester.com/NHFTU85530

OR

https://dotnetfiddle.net/GWWNgb

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I am a beginner. What is case 'Section One' ? What I have to change it to?
@Eddga : What are you doing ! I said in your case you don't need for custom method for sorting ! just need to use like this : var list = List_unsort.OrderBy(x => x.subitem); here is a working example : link1 OR link2
Thank you. This answer also working too. But I am sorry that it can approve only 1 answer. :(
0

Here is a very simple example using the ListViewItemSorter property. It assumes that distance is ALWAYS the third column (second subitem):

private class ItemComparer : IComparer
{
    public int Compare(object x, object y)
    {
        // Convert objects to ListViewItems
        ListViewItem i1 = x as ListViewItem;
        ListViewItem i2 = y as ListViewItem;

        // If converstion to ListViewItems failed, return -1 (less than)
        if (i1 == null) return -1;
        if (i2 == null) return -1;

        // If ListViewItems DO NOT have the correct number of subitems, return -1 (less than)
        if (i1.SubItems.Count < 2) return -1;
        if (i2.SubItems.Count < 2) return -1;

        // Convert subitem 2 (index 1) text to a numeric value
        double distance1, distance2;
        if (!Double.TryParse(i1.SubItems[1].Text, out distance1)) return -1;
        if (!Double.TryParse(i2.SubItems[1].Text, out distance2)) return -1;

        // Compare the distance1 to distance2 (-1 = less than, 0 = equal, 1 = greater than)
        return distance1.CompareTo(distance2);
    }
}


public Form1()
{
    InitializeComponent();

    listView1.Items.Add(new ListViewItem(new string[] { "Flick", "20", "1.4" }));
    listView1.Items.Add(new ListViewItem(new string[] { "John",  "19", "1.0" }));
    listView1.Items.Add(new ListViewItem(new string[] { "Hamn", "19", "0.9" }));

    listView1.ListViewItemSorter = new ItemComparer();
    listView1.Sort();
}

9 Comments

Thanks. But I have some question. Where is the point that will check item.subitem[2] (distance).
I added comments to better explain the custom comparer. When you call sort() on the list view, it will call the comparer with "pairs" of the ListViewItem that you have added to determine their sort order.
I can't create a new class. Its say error at IComparer.
What's the error? You will need a "using System.Collections" statement to "include" the IComparer interface.
If I want it to check the item.subitems[7]. So i have to change (i1.SubItems.Count < 2) to (i1.SubItems.Count < 7) and i1.SubItems[1].Text to i1.SubItems[6].Text? Is that correct?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.