Solved

# Correct ordering

Posted on 2016-11-06
42 Views
Hi All

Example: I have data in a field like this:
AA001162
AAAAAAA
AABBBBB
AAZZZZZ
AA12345
B1.2
B1.12
C10
C2
D10
D2
I want it to be sorted as this:
AAAAAAA
AABBBBB
AAZZZZZ
AA001162
AA12345
B1.12
B1.2
C10
C2
D10
D2
0
Question by:DatabaseDek
• 5
• 4
• 3
• +1

LVL 49

Expert Comment

You can do that in a query with

Order By
Left([DoorCode], 2),
Val("9" & Mid([DoorCode], 3))

/gustav
0

LVL 30

Expert Comment

Isn't this the correct required output?
AA001162
AA12345
AAAAAAA
AABBBBB
AAZZZZZ
B1.2
B1.12
C2
C10
D2
D10
0

Author Comment

I will try that Gustav

Hnasr
Actually that would more than suffice.
Ideally
B1.2 should come after
B1.12

But what you have is much better than what I get
0

LVL 30

Expert Comment

Input:
01      AA001162
02      AAAAAAA
03      AABBBBB
04      AAZZZZZ
05      AA12345
06      B1.2
07      B1.12
08      C10
09      C2
10      D10
11      D2

Required Output:
01      AA001162
05      AA12345
02      AAAAAAA
03      AABBBBB
04      AAZZZZZ
06      B1.2
07      B1.12
09      C2
08      C10
11      D2
10      D10

Process:
Convert each numeric piece to a 10 digit string by adding 0's to beginning of number.
Rejoin strings and sort.

Query:
``````SELECT a.aID, a.adesc
FROM a
``````
sort-alpha-numeric.accdb
0

LVL 49

Expert Comment

A solution is not possible if you neither can define nor decide how to sort.

/gustav
1

LVL 47

Accepted Solution

Dale Fye (Access MVP) earned 250 total points
It would take longer to run, but you could create two functions, one to pull back the Alpha portion of the field and the second to pull back the Numeric portion.  Then you would sort by the returned value of these two functions.

Since your #'s all appear to be at the end, you could strip them out with:
``````Public Function StripTrailingNumbers(ByVal SomeVal as String) as String

Do
if isnumeric(Right(SomeVal, 1)) then
SomeVal = Left(SomeVal, Len(SomeVal)-1)
Else
Exit Do
End If
Loop

StripTrailingNumbers  = SomeVal

End Function
``````
Then the second function would return a numeric so that the numbers sort properly:

``````Public Function TrailingNumbers(SomeVal as string) as Double

Dim intLoop as integer

For intLoop = 1 to Len(SomeVal)
if isnumeric(Mid(SomeVal, intLoop)) Then
TrailingNumbers = Val(Mid(SomeVal, intLoop))
Exit Function
endif
Next

TrailingNumbers = 0

End Function
``````

Then, your query would look something like:

SELECT [yourField]
FROM yourTable
ORDER BY StripTrailingNumbers([yourField]), TrailingNumbers([yourField])
0

Author Comment

Gustav

You cannot decide what sorting system you need when you do not know and when your customer does not know what he is going to get from his customer. It is simply a case of getting the best guess. Or perhaps just something better than the standard access stuff.

Hnasr

I cannot open your file I am still on Access 2002. Can you convert it please.

Dale

I will give that a try. Rushing to finish something this week!!!
0

LVL 30

Assisted Solution

hnasr earned 250 total points
Here it is in mdb format.
sort-alpha-numeric.mdb
0

LVL 49

Expert Comment

It is simply a case of getting the best guess.

Well, then my straight solution is the best, simple as it is. It fulfills your example data and sorting.

/gustav
0

LVL 30

Expert Comment

My comment was based on this challenging requirement:

customer can enter door numbers with any alpha numeric combination, inc decimal points anywhere. So writing code or a function may be too much

https://www.experts-exchange.com/questions/28979884/Orderby-function.html#a41867276

Although it handles the simple ABC123 entry, I may assume entries like,
A1B2C3D
A01X
A2
2A
2A10
A10
Which sorts to:
2A
2A10
A1B2C3D
A01X
A2
A10
0

Author Closing Comment

Brilliant Guys. Thank you
0

Author Comment

How do I sort using these functions as "Desc"
0

LVL 47

Expert Comment

ARe you talking to me?

ORDER BY StripTrailingNumbers([yourField]), TrailingNumbers([yourField]) DESC

or if you want the Alpa part descending as well

ORDER BY StripTrailingNumbers([yourField]) DESC, TrailingNumbers([yourField]) DESC
0

LVL 30

Expert Comment

Descending order:
``````SELECT a.aID, a.adesc, findAlphaNumeric([adesc])
FROM a
``````
Modified function to properly sort entries such as 01X, and 1A .
-sort-alpha-numeric-2.mdb
0

## Featured Post

### Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…