Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort Twice

Status
Not open for further replies.

Quan9r

Technical User
May 18, 2000
36
US
I have a four digit number that I would like to sort by the last two numbers - which I can do through a string - but after I sort by the last two I would like to sort by the first two
so they will be grouped by the last two but in ascending order by the first two - Make sense?
i.e.
1201
1301
1801
Any help would be appreciated!!!
 
Just use the Right([number],2) function in your query to sort the last 2, then you can use the whole number for your 2nd sort. Maq B-)
<insert witty signature here>
 
Hmmmmmmmmmmmm,

Some 'issues' here?

Quan9r, If you use the second two digits in a string sort, the order will be different than a numeric sort of the same, so YOU need to know which sort order you want.

Maquis , NO. Please re-read the post. By &quot;... sort by the last two numbers ... &quot; Quan9r is clearly meaning to sort by the RIGHT pair of digits, with a sub-sort on the left pair of digits. Further, you never explained HOW to do the double sort, just told him to &quot;do it&quot;.


blavuma, see comment to Quan

I do not think I have 'all the facts' for this, especially wheather the 'sort' should be numeric or string based, but also wheather the item list is a recordset or just some collection generated dynamically or from 'elsewhere' and just used.

The first option which occurs to me is to generate a companion value with the pair of 'digits; swapped', and then sort on that companion, but again, the issues noted above (esp. the alpha-Numeric) have an impact on this implementation.

The simple procedure will do the swapping - wit the option for a numeric or string value. Without more details of the source / destination of the process, attempting to provide the actual sort mechanics is more than I care to take on, and it is probably within Quan9r's capability anyway.

Code:
Public Function SwapDigPairs(ByVal intFourNum As Variant, Optional blnAnFlg = False) As Variant

    'Swaps the digit pairs of a four digit Number

    'Usage
    '(Numeric Return; w/o Flag)
    '? SwapDigPairs(1201)
    ' 112

    '(Alpha Return; w/Flag)
    '? SwapDigPairs(1201, True)
    '0112


    Dim tmpLftPr As Integer
    Dim tmpRgtPr As Integer

    tmpLftPr = intFourNum \ 100
    tmpRgtPr = intFourNum - 100 * tmpLftPr

    If (blnAnFlg = True) Then
        SwapDigPairs = Format(CStr(tmpRgtPr), &quot;00&quot;) & Format(CStr(tmpLftPr), &quot;00&quot;)
     Else
        SwapDigPairs = tmpRgtPr * 100 + tmpLftPr
    End If

End Function



MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top