473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sorting Numbers within A String

Hello,
This is my first post, so I hope I include all the right information.

I have a Table which has a list of numbers stored in a string, which are not
in numerical order
For example "10 2 35 21 42 3 6" etc

My aim is to create a function which will put these in to numerical order
like "2 3 6 10 21 35 42"

If anyone could point me in the right direction it would be greatly
appreciated.

Thank you in advance
TituscroW
Nov 13 '05 #1
3 1870
If there are ONLY numbers in this field....then why is it a string??
that would make it a whole lot simpler...

The other Option is to bring the table into a query..
and make a calculated field with the VAL function.
something like... NumericalValue: Val(theFieldName)
and then sort this on this column.

IF you mean that each record has a string with a list of numbers "2 3 6
10" etc. and you want to pull it apart, order it, and put it back
together....then that would take a little more work - probably
something like...

* break up the string into a variable array using the Left, Mid, Right
functions (or the ExtractStr function below)
* sort the array
* rebuild the string.

Hope this helps,
Mal.

Beware of work wrap in this copied/pasted example.
==================================================
Function ExtractStr(strIn, intNeedSegment, strDelimiter, Optional
strNotFound As String) As String

' Function to chop a input string into segments and return the
requested segment
' Written and developed by Thomas M. Brittell
' Copyright 1998; All rights reserved.
'
' strIn - Input string to be segmented
' intNeedSegment - Indicates the segment to be returned
' strDelimiter - The delimiter used to seperate each segment
' strNotFound - When no segment is found return the specified
string if provided
'
'** USAGE: extractStr(mystr,5,",","Not Found")
' from "hello,this,is,my,joined,string'
' would return "joined"

Dim intCurrentPosition As Integer
Dim intFoundPosition As Integer
Dim intLastPosition As Integer
Dim intGetSegment As Integer
Dim wrkNotFound As String

If IsEmpty(strNotFound) Or strNotFound = "" Then
wrkNotFound = ""
Else
wrkNotFound = strNotFound
End If

If strIn = "" Or IsNull(strIn) = True Then strIn = ""

intCurrentPosition = 0
intFoundPosition = 0
intLastPosition = 0
intGetSegment = intNeedSegment

Do While intGetSegment > 0
intLastPosition = intCurrentPosition
'Find a occurance of the delimiter
intFoundPosition = InStr(intCurrentPosition + 1, strIn,
Left$(strDelimiter, 1))
If intFoundPosition > 0 Then
intCurrentPosition = intFoundPosition
intGetSegment = intGetSegment - 1
Else
'End of input string so exit
intCurrentPosition = Len(strIn) + 1
Exit Do
End If
Loop
'If nothing was found and you had at least one delimiter return ""
If (intFoundPosition = 0) And ((intGetSegment <> intNeedSegment)
And (intGetSegment > 1)) Then
ExtractStr = wrkNotFound
Else
'Return the segment between the last position and the current
one
ExtractStr = Mid$(strIn, intLastPosition + 1,
intCurrentPosition - intLastPosition - 1)
End If

End Function

Nov 13 '05 #2
Thanks Mal,

I will apply this function and let you know how I go.

The string is all in numbers so I can put each number into individual fields.
However, I think I have the same issue.
I can compare any one field to another and move it left or right accordingly.
But I have trouble doing this for several fields (Numbers) over 1,000's of
records.

Thanks again
TituscroW
Nov 13 '05 #3
You should be able to use the Split function to separate the text numbers
into an array. There are various sort algorithms documented (Google this and
various VB newsgroups for references) to sort arrays -- get one and use it.
Then, if you feel compelled to continue the non-relational "multi-value"
field, format each and concatenate it back into a string. If you don't feel
so compelled, just write them to a table, with a foreign key pointing back
to the original record. Then you will sort them in the Query you use to
retrieve them from that table.

Larry Linson
Microsoft Access MVP

"TituscroW" <u15606@uwe> wrote in message news:5720333786182@uwe...
Thanks Mal,

I will apply this function and let you know how I go.

The string is all in numbers so I can put each number into individual
fields.
However, I think I have the same issue.
I can compare any one field to another and move it left or right
accordingly.
But I have trouble doing this for several fields (Numbers) over 1,000's of
records.

Thanks again
TituscroW

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
6
by: carlos123 | last post by:
I have a programming assignement that i have been working on for quite some time now. I need your guys' help. My assignement is to great a table with data in it. and it will have a combobox and 2...
7
beacon
by: beacon | last post by:
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes to um, sort an array. I have run into some...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.