Sort a Custom Class Collection in VB6
-
Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:
Class myClass
private ID as String
private Name as String
private TK as String
private Di as String
private M as StringEnd Class
Using this class i add to a Collection a number of items, e.g.
Private colClass As Collection
Private clsTheClass As myClassSet colClass = New Collection
Set clsTheClass = New myClass clsTheClass.Name = "A" clsTheClass.TKs = "100" clsTheClass.Di = "5" clsTheClass.ID = "1" clsTheClass.M = "9" colClass.Add clsTheClass
What i end up with it is a collection that looks like so after adding all the items:
ID |Name |TK |Di |M
1 A 100 5 9
2 B 100 3 9
3 C 10 5 9
4 D 10 7 9
5 E 10 1 9
6 F 400 9 5
7 F 400 4 5I need to sort the collection On TK, and then DI. That should make the collection look like this:
ID |Name |TK |Di |M
6 F 400 9 5
7 F 400 4 5
1 A 100 5 9
2 B 100 3 9
4 D 10 7 9
3 C 10 5 9
5 E 10 1 9I am currently using this method to sort on TK, but i cannot get it to work how i need the output:
Public Sub SortCollection(ColVar As Collection)
Dim oCol As Collection
Dim i As Integer
Dim i2 As Integer
Dim iBefore As IntegerIf Not (ColVar Is Nothing) Then If ColVar.Count > 0 Then Set oCol = New Collection For i = 1 To ColVar.Count If oCol.Count = 0 Then oCol.Add ColVar(i) Else iBefore = 0 For i2 = oCol.Count To 1 Step -1 If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then iBefore = i2 Else Exit For End If Next If iBefore = 0 Then oCol.Add ColVar(i) Else oCol.Add ColVar(i), , iBefore End If
-
Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:
Class myClass
private ID as String
private Name as String
private TK as String
private Di as String
private M as StringEnd Class
Using this class i add to a Collection a number of items, e.g.
Private colClass As Collection
Private clsTheClass As myClassSet colClass = New Collection
Set clsTheClass = New myClass clsTheClass.Name = "A" clsTheClass.TKs = "100" clsTheClass.Di = "5" clsTheClass.ID = "1" clsTheClass.M = "9" colClass.Add clsTheClass
What i end up with it is a collection that looks like so after adding all the items:
ID |Name |TK |Di |M
1 A 100 5 9
2 B 100 3 9
3 C 10 5 9
4 D 10 7 9
5 E 10 1 9
6 F 400 9 5
7 F 400 4 5I need to sort the collection On TK, and then DI. That should make the collection look like this:
ID |Name |TK |Di |M
6 F 400 9 5
7 F 400 4 5
1 A 100 5 9
2 B 100 3 9
4 D 10 7 9
3 C 10 5 9
5 E 10 1 9I am currently using this method to sort on TK, but i cannot get it to work how i need the output:
Public Sub SortCollection(ColVar As Collection)
Dim oCol As Collection
Dim i As Integer
Dim i2 As Integer
Dim iBefore As IntegerIf Not (ColVar Is Nothing) Then If ColVar.Count > 0 Then Set oCol = New Collection For i = 1 To ColVar.Count If oCol.Count = 0 Then oCol.Add ColVar(i) Else iBefore = 0 For i2 = oCol.Count To 1 Step -1 If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then iBefore = i2 Else Exit For End If Next If iBefore = 0 Then oCol.Add ColVar(i) Else oCol.Add ColVar(i), , iBefore End If
-
Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:
Class myClass
private ID as String
private Name as String
private TK as String
private Di as String
private M as StringEnd Class
Using this class i add to a Collection a number of items, e.g.
Private colClass As Collection
Private clsTheClass As myClassSet colClass = New Collection
Set clsTheClass = New myClass clsTheClass.Name = "A" clsTheClass.TKs = "100" clsTheClass.Di = "5" clsTheClass.ID = "1" clsTheClass.M = "9" colClass.Add clsTheClass
What i end up with it is a collection that looks like so after adding all the items:
ID |Name |TK |Di |M
1 A 100 5 9
2 B 100 3 9
3 C 10 5 9
4 D 10 7 9
5 E 10 1 9
6 F 400 9 5
7 F 400 4 5I need to sort the collection On TK, and then DI. That should make the collection look like this:
ID |Name |TK |Di |M
6 F 400 9 5
7 F 400 4 5
1 A 100 5 9
2 B 100 3 9
4 D 10 7 9
3 C 10 5 9
5 E 10 1 9I am currently using this method to sort on TK, but i cannot get it to work how i need the output:
Public Sub SortCollection(ColVar As Collection)
Dim oCol As Collection
Dim i As Integer
Dim i2 As Integer
Dim iBefore As IntegerIf Not (ColVar Is Nothing) Then If ColVar.Count > 0 Then Set oCol = New Collection For i = 1 To ColVar.Count If oCol.Count = 0 Then oCol.Add ColVar(i) Else iBefore = 0 For i2 = oCol.Count To 1 Step -1 If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then iBefore = i2 Else Exit For End If Next If iBefore = 0 Then oCol.Add ColVar(i) Else oCol.Add ColVar(i), , iBefore End If
Whenever I sort, I find the largest, or the least first and set the value to "x". From there I work up, or down assigning to a temp object, collection or array, and replacing "x" as the largest each time. For example, x = 0 for each item in collection if item > x then x = item end if next This gets the largest to begin. tempCollection.add(x) while tempcollection.count isnot collection.count for each item in collection set x if the item is less than the last in tempcollection and is not less than x next tempcollection.add(x) loop That is a level one sort, but you should be able to expand to code easily enough. Sorry that I lack any time, or I would have done it for you!
-
Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:
Class myClass
private ID as String
private Name as String
private TK as String
private Di as String
private M as StringEnd Class
Using this class i add to a Collection a number of items, e.g.
Private colClass As Collection
Private clsTheClass As myClassSet colClass = New Collection
Set clsTheClass = New myClass clsTheClass.Name = "A" clsTheClass.TKs = "100" clsTheClass.Di = "5" clsTheClass.ID = "1" clsTheClass.M = "9" colClass.Add clsTheClass
What i end up with it is a collection that looks like so after adding all the items:
ID |Name |TK |Di |M
1 A 100 5 9
2 B 100 3 9
3 C 10 5 9
4 D 10 7 9
5 E 10 1 9
6 F 400 9 5
7 F 400 4 5I need to sort the collection On TK, and then DI. That should make the collection look like this:
ID |Name |TK |Di |M
6 F 400 9 5
7 F 400 4 5
1 A 100 5 9
2 B 100 3 9
4 D 10 7 9
3 C 10 5 9
5 E 10 1 9I am currently using this method to sort on TK, but i cannot get it to work how i need the output:
Public Sub SortCollection(ColVar As Collection)
Dim oCol As Collection
Dim i As Integer
Dim i2 As Integer
Dim iBefore As IntegerIf Not (ColVar Is Nothing) Then If ColVar.Count > 0 Then Set oCol = New Collection For i = 1 To ColVar.Count If oCol.Count = 0 Then oCol.Add ColVar(i) Else iBefore = 0 For i2 = oCol.Count To 1 Step -1 If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then iBefore = i2 Else Exit For End If Next If iBefore = 0 Then oCol.Add ColVar(i) Else oCol.Add ColVar(i), , iBefore End If