Sorting list view control by numbers

This is a discussion on Sorting list view control by numbers within the Windows Programming forums, part of the Platform Specific Boards category; I have a column which consists of numbers. I want them to be sorted as numbers (I don't want to ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    could you set " 9" istead of "9" ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,059
    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,059
    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 08: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, 09: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, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21