I need to handle a List (or Array or DataTable Field) composed by numbers where sometimes there are "bis" "ter" "quater" and so on.
The list looks like:
1
2
3
3 bis
4
5
5 bis
5 ter
5 quater
6
...
My idea (to preserve order) is to store data using decimals this way:
5 bis = 5.2
5 ter = 5.3
5 quater = 5.4
and convert to string and then use replace
on extracting data.
My question are:
- is this a good way?
- are there better solutions?
Your approach will break when you get to decies
(ie 5.10
). It may be better to create a new structured type. For example
Public Structure OrderedNumber
Public Value As Integer
Public Order As Integer
End Structure
This lets you do things like :
Module Module1
Public Structure OrderedNumber
Implements IComparable(Of OrderedNumber)
Public Value As Integer
Public Order As Integer
Public Function CompareTo(ByVal other As OrderedNumber) As Integer _
Implements IComparable(Of OrderedNumber).CompareTo
If Value = other.Value Then
Return Order - other.Order
Else
Return Value - other.Value
End If
End Function
End Structure
Sub Main()
Dim OList As New List(Of OrderedNumber)
Dim n As OrderedNumber
' 5 ter - adding out of order!
n.Value = 5
n.Order = 2
OList.Add(n)
' 5
n.Order = 0
OList.Add(n)
' 5 bis
n.Order = 1
OList.Add(n)
OList.Sort()
For Each n In OList
Console.WriteLine(n.Value & ":" & n.Order)
Next
Console.ReadLine()
End Sub
End Module
Which outputs :
5:0
5:1
5:2
You can similarly work with a Dictionary(Of OrderedNumber, MyObject)
, for example, where you want to link each item with a data object of some sort.