Eddga
Eddga

Reputation: 41

C# Sorting Listview

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.

Upvotes: 0

Views: 205

Answers (2)

MrUnknow
MrUnknow

Reputation: 399

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

Upvotes: 1

Marker
Marker

Reputation: 977

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();
}

Upvotes: 0

Related Questions