|
-
Oct 16th, 2013, 04:24 AM
#1
Thread Starter
Fanatic Member
Sorting Order
Hi
I have items with code as alphanumeric . For e.g I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11,....I20,I21.
In listview i am displaying code & name. When i click on code it sorts like this I1,I10,I11,I2 whereas i want it should display like this I1,I2, & so on.
Thanks
-
Oct 16th, 2013, 04:37 AM
#2
Re: Sorting Order
Simple:-
vbnet Code:
' Dim values As String() = New String() {"I1", "I23", "I14", "I12", "I1", "I3", "I11"} values = values.OrderBy(Function(s) CInt(s.Substring(1))).ToArray
EDIT
Oh God...I just realized this was a VB6 question and you also implied that you have a sort already. Well the idea in the above code is that you sort on the numbers by extracting them from each of the strings and converting them into numbers.
How are you sorting ? Do you have some routine that sorts ? If so, post it up.
Last edited by Niya; Oct 16th, 2013 at 04:44 AM.
-
Oct 16th, 2013, 04:40 AM
#3
Thread Starter
Fanatic Member
Re: Sorting Order
Hi Niya
Records can be in hundreds or thousands
Thanks
-
Oct 16th, 2013, 04:47 AM
#4
Re: Sorting Order
Did you see my post edit ?
-
Oct 16th, 2013, 04:52 AM
#5
Re: Sorting Order
At the moment you are getting an alphabetical sort such as you would get in a dictionary of words.
To sort 'codes' like these numerically you should pad them out to equal length; this is typically done with leading zeroes as in;
0I1,0I2,0I3,0I4,0I5,0I6,0I7,0I8,0I9,I10,I11,....I20,I21
but you can use any character you wish.
Oooops!
Correction: I01,I02,I03,I04,I05,I06,I07,I08,I09,I10,I11,....I20,I21
Last edited by Magic Ink; Oct 17th, 2013 at 05:40 PM.
Reason: Oooops!
-
Oct 16th, 2013, 04:53 AM
#6
Thread Starter
Fanatic Member
Re: Sorting Order
Hi
This is the code
Dim itmX As ListItem
Dim intcount As Integer
lvwitem.ListItems.Clear
For intcount = 0 To rstitem.RecordCount - 1
Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
itmX.Text = rstitem("icode")
itmX.SubItems(1) = rstitem("idesc")
rstitem.MoveNext
Next intcount
If rstitem.RecordCount > 0 Then
rstitem.MoveFirst
End If
rsitem is Items table recordset
Thanks
-
Oct 16th, 2013, 04:58 AM
#7
Re: Sorting Order
Wait a minute, you're getting this from a database ? What SQL Select statement are you using to fill that RecordSet ? You might just be able to palm that sort off to database engine which is far better at sorting than any implementation you can provide.
-
Oct 16th, 2013, 05:09 AM
#8
Thread Starter
Fanatic Member
Re: Sorting Order
Hi Niya
It is like this
rstitem.Open "select * from itemmaster", cnn1, adOpenStatic, adLockOptimistic
Thanks
-
Oct 16th, 2013, 05:29 AM
#9
Re: Sorting Order
Use this Select statement instead:-
SQL Code:
select * from itemmaster order by Cast(substring(icode,2,len(icode) - 1) as int)
I'm assuming the column in question is icode and the database is SQL Server.
-
Oct 16th, 2013, 05:35 AM
#10
Thread Starter
Fanatic Member
Re: Sorting Order
Hi
It is giving me error Undefined function cast in expression
rstitem.Open "SELECT * FROM itemmaster ORDER BY Cast(substring(icode,2,len(icode) - 1))", cnn1, adOpenStatic, adLockOptimistic
Thanks
-
Oct 16th, 2013, 05:37 AM
#11
Re: Sorting Order
What database are you using ?
-
Oct 16th, 2013, 05:46 AM
#12
Thread Starter
Fanatic Member
Re: Sorting Order
Hi
I am using MS-Access
Thanks
-
Oct 16th, 2013, 05:57 AM
#13
Re: Sorting Order
Use this instead then:-
sql Code:
SELECT * FROM itemmaster ORDER BY CLng(Mid(icode,2,len(icode) - 1))
-
Oct 16th, 2013, 04:05 PM
#14
Re: Sorting Order
 Originally Posted by Niya
Simple:-
vbnet Code:
'
Dim values As String() = New String() {"I1", "I23", "I14", "I12", "I1", "I3", "I11"}
values = values.OrderBy(Function(s) CInt(s.Substring(1))).ToArray
Oh God...I just realized this was a VB6 question ...
Yeah - sure ...
Here's some similar thing in VB6, using vbRichClient5:
Code:
Dim Arr As cArrayList
Set Arr = New_c.ArrayList(vbString, "XZ 1", "XA 1", "I23", "I14", "I2", "I1", "I3", "X-11", "X-1")
Arr.Sort cmpLogical
then followed by a Print-Out of contents:
Code:
Dim i As Long
For i = 0 To Arr.Count - 1
Debug.Print Arr(i)
Next
gives the following "logically" sorted List:
I1
I2
I3
I14
I23
X-1
X-11
XA 1
XZ 1
Olaf
-
Oct 16th, 2013, 11:37 PM
#15
Thread Starter
Fanatic Member
Re: Sorting Order
Hi Niya
Recordset is o.k
rstitem.Open "SELECT * FROM itemmaster ORDER BY val(Mid(icode,2,len(icode) - 1))", cnn1, adOpenStatic, adLockOptimistic
But in Listview it is not displaying as per recordset
Dim itmX As ListItem
Dim intcount As Integer
lvwitem.ListItems.Clear
For intcount = 0 To rstitem.RecordCount - 1
Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
itmX.Text = rstitem("icode")
itmX.SubItems(1) = rstitem("idesc")
rstitem.MoveNext
Next intcount
If rstitem.RecordCount > 0 Then
rstitem.MoveFirst
End If
Thanks
-
Oct 17th, 2013, 02:58 AM
#16
Re: Sorting Order
 Originally Posted by Schmidt
Yeah - sure ...
Here's some similar thing in VB6, using vbRichClient5:
Code:
Dim Arr As cArrayList
Set Arr = New_c.ArrayList(vbString, "XZ 1", "XA 1", "I23", "I14", "I2", "I1", "I3", "X-11", "X-1")
Arr.Sort cmpLogical
then followed by a Print-Out of contents:
Code:
Dim i As Long
For i = 0 To Arr.Count - 1
Debug.Print Arr(i)
Next
gives the following "logically" sorted List:
I1
I2
I3
I14
I23
X-1
X-11
XA 1
XZ 1
Olaf
I was wondering how long it would take you to bite 
 Originally Posted by Jagjit
Hi Niya...Recordset is o.k.....But in Listview it is not displaying as per recordset
lol one problem at a time....Did my altered Select query sort according to your need ? You didn't confirm that.
-
Oct 17th, 2013, 03:01 AM
#17
Thread Starter
Fanatic Member
Re: Sorting Order
Hi Niya
There is no problem in select statement . It gives me correct results when i see the results in below code using msgbox that is also correct but problem is when it displays in Listview it is not correct.
lvwitem.ListItems.Clear
For intcount = 0 To rstitem.RecordCount - 1
Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
itmX.Text = rstitem("icode")
itmX.SubItems(1) = rstitem("idesc")
rstitem.MoveNext
Next intcount
If rstitem.RecordCount > 0 Then
rstitem.MoveFirst
End If
Thanks
-
Oct 17th, 2013, 03:05 AM
#18
Re: Sorting Order
So this is another problem entirely ?
-
Oct 17th, 2013, 10:26 AM
#19
Frenzied Member
Re: Sorting Order
Hi ,Can you post the screenshot of msgbox . any way can you try the following way .
Code:
rstitem.Open "SELECT * FROM itemmaster ORDER BY val(Mid(icode,2,len(icode) - 1))", cnn1, adOpenStatic, adLockOptimistic
If rsitem.state=adstateopen then
if not (rsitem.eof and rsitem.bof) then 'if record found then
Dim itmX As ListItem
Dim intcount As Integer
lvwitem.ListItems.Clear
For intcount = 0 To rstitem.RecordCount - 1
Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
itmX.Text = rstitem("icode")
itmX.SubItems(1) = rstitem("idesc")
rstitem.MoveNext
Next intcount
'If rstitem.RecordCount > 0 Then 'it does not seems to check something found then Moving pointer at very first record of the recordset
'rstitem.MoveFirst
'End If
end if
End if
-
Oct 21st, 2013, 10:24 AM
#20
New Member
Re: Sorting Order
Since you are using a personalized sorting in the SQL Query, you must ensure that the Sorted Property of the ListView Control is False (other way the listview will re-sort in the normal text string mode and you will have the list incorrectly sorted again).
Code:
lvwItem.Sorted = False
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|