Results 1 to 13 of 13

Thread: Sort Numbers in order in listview?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Sort Numbers in order in listview?

    Hi guys, I have a list view which has numbers in subitems(3)

    now how can I make it so when Command1 is pressed it will sort by highest Value first. at the moment when I sort it it sorts by the first number not the entire value.

    For example

    99000
    9
    9
    9
    8
    8
    8
    80000


    I'd like it in order like

    99000
    80000
    9
    9
    9
    8
    8
    8

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Sort Numbers in order in listview?

    Any ideas?

    Thanks
    Jamie

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Sort Numbers in order in listview?

    because what's in a list view is just text... it doesn't know that what's in the cells is numerical, so it's doing a string sort ... there are two ways to overcome this... 1 - you handle the sort, sort your data accordingly, clear the list view and re-load it with everything in the "right" order. 2 - you pad your shorter numbers with 0's .... so it becomes like this:
    99000
    80000
    00009
    00009
    00009
    00008
    00008
    00008


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Sort Numbers in order in listview?

    SubItems are a Variant/String, so the sorting goes along those lines.

    To sort by value you have two Options (As far as i know)
    1) Transfer the ListView-Entries to a corresponding Array and pass it to a sorting function (e.g. Quicksort) incl. forcechanging the sorting criteria to Numericals, then repopulate your ListView
    2) Determine the length of the longest "string" in SubItems(3) and add leading zeros to the "shorter" entries.

    Maybe someone else has a better idea.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Sort Numbers in order in listview?

    Hi guys, thanks for the replies,

    Do you have any example codes?

    Regards,
    Jamie

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Sort Numbers in order in listview?

    For which option?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Sort Numbers in order in listview?

    Now, if you want to see the data as it is
    What I do is add a new column to the ListView control, set width = 0
    Store the formatted data in that 'hidden' column, formatted as post #3 says
    and define that column as the sorted one
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  8. #8
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: Sort Numbers in order in listview?

    you can try this.. listview1.listitems.add=format(text1.text,"0000000000")..all must have the same digits to get it sorted

  9. #9
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: Sort Numbers in order in listview?


  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Sort Numbers in order in listview?

    Zvoni, either example codes would be handy, this is killing me!

    Madnessman, that code is used for dates, as appose to numbers / values.

  11. #11
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Sort Numbers in order in listview?

    Jamie,

    i'm going for the second option.

    What's your source for the ListView? An Array? a recordset?

    EDIT: I'm retracting. I'm going for the first option
    Last edited by Zvoni; Oct 9th, 2012 at 10:11 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sort Numbers in order in listview?

    The simplest way is to right align the Column, add the data and then sort. Vis:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim li As ListItem
    Dim intI As Integer
    lv.View = lvwReport
    lv.ColumnHeaders.Add , , "Col1"
    lv.ColumnHeaders.Add , , "Col2", , lvwColumnRight
    Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
    li.SubItems(1) = "8"
    intI = intI + 1
    Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
    li.SubItems(1) = "80000"
    intI = intI + 1
    Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
    li.SubItems(1) = "5"
    intI = intI + 1
    Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
    li.SubItems(1) = "2"
    intI = intI + 1
    Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
    li.SubItems(1) = "90000"
    intI = intI + 1
    End Sub
    
    Private Sub lv_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    lv.SortKey = ColumnHeader.Index - 1
    lv.SortOrder = lvwDescending
    lv.Sorted = True
    End Sub
    EDIT: It wont work if you're trying to sort on the very first column, as, for reasons I don't understand, the first column of a ListView must be left aligned.

    EDIT2: If you still want the column to be left aligned then you can just use
    Code:
    Private Sub lv_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    lv.ColumnHeaders.Item(2).Alignment = lvwColumnRight
    lv.SortKey = ColumnHeader.Index - 1
    lv.SortOrder = lvwDescending
    lv.Sorted = True
    lv.ColumnHeaders.Item(2).Alignment = lvwColumnLeft
    End Sub
    Last edited by Doogle; Oct 10th, 2012 at 12:15 AM.

  13. #13
    New Member
    Join Date
    Oct 2012
    Posts
    7

    Re: Sort Numbers in order in listview?

    try this code
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width