By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,215 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,215 IT Pros & Developers. It's quick & easy.

Sorting Number Type

P: 48
Hello,

I am currently using Listview control.
The sorting feature of the control is useful.
Expand|Select|Wrap|Line Numbers
  1. <ListviewCtrlName>.Sorted = True
  2. <ListviewCtrlName>.SortKey = <FieldToSort>
  3. <ListviewCtrlName>.SortOrder = <SortOrder>
It sorts well with Text type but is weird when sorting Number Type.
Ex.
(before sorting)
[1st] 3
[2nd] 2
[3rd] 11
[4th] 4
[5th] 1

(after sorting)
[1st] 1
[2nd] 11
[3rd] 2
[4th] 3
[5th] 4

The correct one should be:
[1st] 1
[2nd] 2
[3rd] 3
[4th] 4
[5th] 11

How to sort number type correctly?
Apr 28 '09 #1
Share this Question
Share on Google+
13 Replies


ADezii
Expert 5K+
P: 8,679
@keirnus
If you set the SortKey Property = 0 it will Sort using the ListItem Object's Text property. Try setting it to a Value between 1 and Number of ColumnHeaders - 1 to Sort using the SubItem whose Collection Index you specify, then see what happens.
Apr 28 '09 #2

P: 48
@ADezii
(I edited this reply coz I was wrong...sorry)

Thanks for the reply ADezii.

But I noticed something. The SortKey is the column index in listview.
It starts with zero(0) so in my example, since there's only 1 column so
it starts with index 0. If I got 3 columns and want to sort the 2nd, I will
be using index 1. (please correct me if I'm wrong)

I tried sorting the column with number type and yet the sorting is wrong.
The sort is treating it like an alpha when it should be numeric.
Apr 28 '09 #3

ADezii
Expert 5K+
P: 8,679
@keirnus
As soon as I get a chance, I'll create a DB with a ListView incorporated in a Form and see if I can 'Sort' the problem (LOL)!
Apr 28 '09 #4

NeoPa
Expert Mod 15k+
P: 31,707
It looks like you're simply sorting on the string value, instead of the numeric value.
Apr 28 '09 #5

NeoPa
Expert Mod 15k+
P: 31,707
@keirnus
Which values are you actually using?
Apr 28 '09 #6

ADezii
Expert 5K+
P: 8,679
@keirnus
Keirnus, I do see your dilemma in that the ListView Control seems to always want to do an Alpha Sort on Column Headers regardless of the Data Type. I'll look into it further at a later Date, but I do have a solution at this time.
  1. In the ColumnClick() Event of the ListView Control, if the Index of the ColumnHeader is anything but the 1st Column, set the SortKey = ColumnHeader.Index -1 then set the Sorted Property of the ListView = True.
  2. If the Index of the ColumnHeader = Column 1, then:
    1. Clear the ListView Control
    2. Set the Sorted Property = False
    3. Create a RecordSet setting the Primary Sort Order of your Numeric Field to Ascending
    4. Re-populate the ListView based on this Recordset
    5. This entire process will be transparent to the User, and, unless you are dealing with very large Recordsets, will populate quite rapidly.
  3. I created a Demo Database for you, simply download the Attachment, then Click on the Column Headers to set the Sorts for the 3 Columns
Apr 28 '09 #7

NeoPa
Expert Mod 15k+
P: 31,707
@keirnus
I'm confused.

I started by assuming you were referring to a ListBox control, but I didn't find any of these properties there so I stopped and looked again.

I found the ListView control, but even there, I found those properties missing.

I'm using Access 2003. Is it possible you're using a different version?
Apr 29 '09 #8

ADezii
Expert 5K+
P: 8,679
@NeoPa
Hello NeoPa, these Properties definately exist for the ListView Control but not via the Normal Interface, you can access them either by:
  1. Right Click the ListView Control ==> ListViewCtrl Object ==> Properties ==> Sorting Tab
  2. Object Browser ==> Class = MSComctlLib ==> Search for ListView ==> Properties, Methods, and Events will appear in the Right Hand Pane
Apr 29 '09 #9

missinglinq
Expert 2.5K+
P: 3,532
I can't lay my hands on a reference right now, but I'm about 99.9% sure that ListView controls only sort on strings and treat any other datatype as if they were strings when sorting, which is what it's doing here.

Linq ;0)>
Apr 29 '09 #10

ADezii
Expert 5K+
P: 8,679
@missinglinq
I think you hit the nail on the Head, any attempt to Sort on Native Numeric Types, or Data Types coerced to Numeric, seem to fail. It appears as though the ListView must be repopulated as indicated in my prior Post.
Apr 29 '09 #11

NeoPa
Expert Mod 15k+
P: 31,707
@ADezii
Bizarre. It's almost as if I'm looking at a different, unrelated, ListView control.

Method 1 would be default for me. Didn't find anything (not even a sorting tab).
I also tried IntelliSense, but that yielded nothing either.

I'd like to see if I could find a way of coercing the item to a numeric value, but it's hard if I can't even find it.

Never mind.
Apr 29 '09 #12

ADezii
Expert 5K+
P: 8,679
@NeoPa
  1. Are we talking about the same animal, namely:
    Expand|Select|Wrap|Line Numbers
    1. Form Design View ==> Insert ==> ActiveX Control ==> Microsoft ListView Control, version X.X
  2. Did you download the Attachment in Post #7 of this Thread, and is it functional?
Apr 29 '09 #13

NeoPa
Expert Mod 15k+
P: 31,707
  1. I used the Toolbox button and More tools. If this is the same I don't know.
  2. No. I rarely download attachments. I don't often have the time to get into questions that deeply, and I never download anything at my work PC.
PS. It's not important if I don't know too much about this control. I was only interested to see if I could help with the question. As you guys seem to be covering it anyway, it's hardly a problem if I'm on a rail track that is sort of parallel, but verges off on its way to a different city.
Apr 29 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.