Thread: Sorting list view control by numbers

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Sorting list view control by numbers

    I have a column which consists of numbers. I want them to be sorted as numbers (I don't want to add 0s in front of them because they can be quite large numbers), not as text. Can I do it without making my own complex sorting system?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    To get "9" before "10", first compare the lengths of the strings to decide on the order.
    If the two lengths are the same, then use strcmp()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yeah but can I do it using the built-in sorting system?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    could you set " 9" istead of "9" ?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If list view control doesn't have such options to sort by numbers then I'm going to do something like that yes.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Use the ListView_SortItems Macro to do all your sorting. This will eliminate the need to do any type of numeric padding.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Perfect! Now is there a way to let it sort some of them automatically by the text?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Now is there a way to let it sort some of them automatically by the text?
    Not sure what you mean by "them". Are you referring to several columns in a listview, some columns are of a "numeric" type and some are of a string type. If this is the case, then your compareProc can be easily modified to sort both types, "numeric" and string. I guess I'm confused by the above statement, please clarify.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You got it right. I just thought maybe there are preconstructed sorting ways too... so I wouldn't need to make my own case insensitive string comparing function.
    Last edited by maxorator; 01-05-2007 at 09:56 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM