Many questions about listview sorting

Source: Internet
Author: User
ArticleDirectory
    •  

Recently, you need to add the FTP function in the software, which is a simple function, but you have encountered many problems. The main problem is directory browsing. How to split the size and modification time from the list returned by FTP is also unexpected. The main reason for using the listview control on the list display is that the listview control "add" data is more natural, while the datagridview is more "bind" data.
Then we found that listview does not have the sorting function.
In fact, there are still some sorting functions, but you only need to specify a comparator.

 
Listview1.listviewitemsorter = xxxcomparer. instance (xxxcomparer: icomparer)

This is my FTP window. Only the file name, size, and modification time are displayed.[Directory name]In principle, the sorting method of file names should be upward, directory, and file. You don't need to talk about the size and modification time.
At this time, the problem arises: The listview item (Subitem) There is no data type.
In this regard, I added a strong type of listviewsubitem to listviewsubitem:

/// <Summary> /// strong-type listviewsubitem /// </Summary> /// <typeparam name = "T"> </typeparam> public class listviewtypesubitem <t>: listviewitem. listviewsubitem {public listviewtypesubitem () {valuetype = typeof (t);} public type valuetype {Get; Set ;}public T value {Get; Set ;}}

Of course, this so-called strong type still has some problems. For now.
With a strong listviewsubitem, you must modify the value assigned to the listview.

Foreach (var file in files) {LVI = new listviewitem (file. isdirectory? String. format ("[{0}]", file. name): file. name); size = new listviewtypesubitem <long> (); size. TEXT = file. size. tostring ("N0"); size. value = file. size; LVI. subitems. add (size); Date = new listviewtypesubitem <datetime> (); date. TEXT = file. createtime. tostring ("yyyy-mm-dd hh: mm"); date. value = file. createtime; LVI. subitems. add (date); LVI. tag = file. isdirectory? Filetypeenum. Directory: filetypeenum. file; lvftpfile. Items. Add (LVI );}

Of course, the important comparator has not been written yet. The comparator I wrote is listed here. It should be a bit of a problem because I didn't want it to be clicked twice. That is to say, I didn't want him to support it once.ASCAgainDesc. Originally, I just wanted to sort the file size and modification time in reverse order, and the file names were arranged according to the rules I mentioned above. Finally, the demand has changed.
After half a day, list my comparator:

/// <Summary> /// listviewitem custom sorting /// process the long and datetime types (DESC) /// for strings, the directory is placed on the file, top-Up: // </Summary> public class listviewitemcomparer: icomparer {Private Static int Col; Private Static bool isdesc = false; Public listviewitemcomparer (INT column) {Col = column;} public int compare (Object X, object y) {int result = 0; var sub = (listviewitem) X ). subitems [col]; If (sub is listviewtypesubitem <Long>) {var sub_x = (listviewitem) X ). subitems [col] As listviewtypesubitem <long>; var sub_y = (listviewitem) Y ). subitems [col] As listviewtypesubitem <long>; If (sub_x.value> sub_y.value) {result = 1;} else if (sub_x.value = sub_y.value) {result = 0 ;} else {result =-1 ;}} else if (sub is listviewtypesubitem <datetime>) {var sub_x = (listviewitem) X ). subitems [col] As listviewtypesubitem <da Tetime>; var sub_y = (listviewitem) Y ). subitems [col] As listviewtypesubitem <datetime>; Result = datetime. compare (sub_y.value, sub_x.value);} else {string sub_x = (listviewitem) X ). subitems [col]. text; string sub_y = (listviewitem) Y ). subitems [col]. text; If (sub_x [0] = '[' & sub_x [sub_x.length-1] = ']') & (sub_y [0]! = '[' | Sub_y [sub_y.length-1]! = ']') {Return-1;} else {result = string. compare (sub_x, sub_y) ;}} var lvi_x = (listviewitem) x; var lvi_y = (listviewitem) y; If (lvi_x.text = "[..] ") {return-1;} // If (lvi_y.text =" [..] ") // {// return isdesc? 1:-1; //} return isdesc? Result: result = 0? 0: Result = 1? -1: 1;} public static listviewitemcomparer sort (INT column = 0) {isdesc =! Isdesc; return New listviewitemcomparer (column );}}

FromCodeYou can better feel the way you have changed.
Self-perception

 
Return isdesc? Result: result = 0? 0: Result = 1? -1: 1;

Someone will say it. Haha.

My original blog address: a lot of questions about listview sorting

Welcome to my blog: nanqi.info

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.