Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Correct ordering

Posted on 2016-11-06
16
Medium Priority
?
70 Views
Last Modified: 2016-12-07
Hi All

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

Expert Comment

by:Gustav Brock
ID: 41876094
You can do that in a query with

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

/gustav
0
 
LVL 31

Expert Comment

by:hnasr
ID: 41876116
Isn't this the correct required output?
adesc
AA001162
AA12345
AAAAAAA
AABBBBB
AAZZZZZ
B1.2
B1.12
C2
C10
D2
D10
0
 

Author Comment

by:DatabaseDek
ID: 41876134
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 31

Expert Comment

by:hnasr
ID: 41876149
Input:
Table a (aid, adesc)
aID      adesc
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:
aID      adesc
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:
Adding a function in modules.
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
ORDER BY finedAlphaNumeric([adesc]);

Open in new window

sort-alpha-numeric.accdb
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 41876150
A solution is not possible if you neither can define nor decide how to sort.

/gustav
1
 
LVL 49

Accepted Solution

by:
Dale Fye earned 1000 total points
ID: 41876404
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

Open in new window

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

Open in new window


Then, your query would look something like:

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

Author Comment

by:DatabaseDek
ID: 41877947
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 31

Assisted Solution

by:hnasr
hnasr earned 1000 total points
ID: 41877963
Here it is in mdb format.
sort-alpha-numeric.mdb
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 41878185
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 31

Expert Comment

by:hnasr
ID: 41878584
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,
adesc
A1B2C3D
A01X
A2
2A
2A10
A10
Which sorts to:
adesc
2A
2A10
A1B2C3D
A01X
A2        
A10
0
 

Author Closing Comment

by:DatabaseDek
ID: 41895777
Brilliant Guys. Thank you
0
 

Author Comment

by:DatabaseDek
ID: 41895791
How do I sort using these functions as "Desc"
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 41895890
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 31

Expert Comment

by:hnasr
ID: 41896185
Descending order:
SELECT a.aID, a.adesc, findAlphaNumeric([adesc])
FROM a
ORDER BY findAlphaNumeric([adesc]) DESC;

Open in new window

Modified function to properly sort entries such as 01X, and 1A .
-sort-alpha-numeric-2.mdb
0
 

Author Comment

by:DatabaseDek
ID: 41916313
Brilliant. Thank you.

Derek
0
 
LVL 31

Expert Comment

by:hnasr
ID: 41916466
Welcome!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question