Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sorting a ListView by more than one column

Sorting a ListView by more than one column

Scheduled Pinned Locked Moved C#
helpalgorithmstutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scotlandc
    wrote on last edited by
    #1

    Hello, Hope someone can help with this, I've built a collection which I'm then using to populate a ListView. I need to somehow be able to sort the ListView by 2 columns, column indexes 3 then 2. The ListView contains information about advertisements in a publication and I need to be able to sort by section (column 4, idx 3) then page no (column 3, idx 2). The thing is, how? I know I need to use the ListViewItemSorter method somehow but I've no idea how to attach the required columns. It will only ever be these 2 columns and they will always have the same position in the ListView (idxs 3 & 2). Hope someone has a work around for this, I've managed to do it in a DataTable but can not get it working here yet. If anyone has a different solution to this problem, please don't be shy. I'm more than open to new ideas. Thanks in advance everyone, Scott

    L L M 3 Replies Last reply
    0
    • S scotlandc

      Hello, Hope someone can help with this, I've built a collection which I'm then using to populate a ListView. I need to somehow be able to sort the ListView by 2 columns, column indexes 3 then 2. The ListView contains information about advertisements in a publication and I need to be able to sort by section (column 4, idx 3) then page no (column 3, idx 2). The thing is, how? I know I need to use the ListViewItemSorter method somehow but I've no idea how to attach the required columns. It will only ever be these 2 columns and they will always have the same position in the ListView (idxs 3 & 2). Hope someone has a work around for this, I've managed to do it in a DataTable but can not get it working here yet. If anyone has a different solution to this problem, please don't be shy. I'm more than open to new ideas. Thanks in advance everyone, Scott

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Have you ever sorted a ListView by one column? If you had I would think the answer would be obvious. http://support.microsoft.com/kb/319401[^]

      1 Reply Last reply
      0
      • S scotlandc

        Hello, Hope someone can help with this, I've built a collection which I'm then using to populate a ListView. I need to somehow be able to sort the ListView by 2 columns, column indexes 3 then 2. The ListView contains information about advertisements in a publication and I need to be able to sort by section (column 4, idx 3) then page no (column 3, idx 2). The thing is, how? I know I need to use the ListViewItemSorter method somehow but I've no idea how to attach the required columns. It will only ever be these 2 columns and they will always have the same position in the ListView (idxs 3 & 2). Hope someone has a work around for this, I've managed to do it in a DataTable but can not get it working here yet. If anyone has a different solution to this problem, please don't be shy. I'm more than open to new ideas. Thanks in advance everyone, Scott

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, same as for any other collection you want to sort (array, ArrayList, ...): write a class that implements IComparer and pass that to the Sort() method; on a LV that happens through setting ListViewItemSorter property. Implementing IComparer means providing a single method that takes two objects and returns an int:

        int Compare (
        Object x,
        Object y
        )

        the int value should be negative, zero or positive depending on the sort order of the two objects; you would have to cast these objects to whatever type it is you have in the ListView.Items collection :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


        1 Reply Last reply
        0
        • S scotlandc

          Hello, Hope someone can help with this, I've built a collection which I'm then using to populate a ListView. I need to somehow be able to sort the ListView by 2 columns, column indexes 3 then 2. The ListView contains information about advertisements in a publication and I need to be able to sort by section (column 4, idx 3) then page no (column 3, idx 2). The thing is, how? I know I need to use the ListViewItemSorter method somehow but I've no idea how to attach the required columns. It will only ever be these 2 columns and they will always have the same position in the ListView (idxs 3 & 2). Hope someone has a work around for this, I've managed to do it in a DataTable but can not get it working here yet. If anyone has a different solution to this problem, please don't be shy. I'm more than open to new ideas. Thanks in advance everyone, Scott

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          Hi! I think one important point that might not be entirely obvious is that you sort by one column first and only have to return the result of the second column's comparison if the items to compare are the same in the first column!

          Regards, mav -- Black holes are the places where God divided by 0...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups