Results 1 to 7 of 7
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    Odd Sorting Behavior?

    So I have my sorting for a report setup as Length with A on top (descending) by entire value with a sub sort of Quantity with A on top by entire value.

    This is the data I am putting in

    Length Quantity
    10 200
    5 850
    5 430
    10 560
    10 440
    6 180
    10 60


    But the order It sorts into is
    Length Quantity
    10 200
    10 440
    10 560
    10 60
    5 430
    5 850
    6 180
    For some reason in the sub sort(quantity) the 10 x 60 comes at the end and in the main sort(length) the 6 comes after the 5's.
    Does anyone know why it acts like this? It doesn't make any sense? 6 it higher than 5 haha.

    Also sometimes length will be something like 50" in which case I want it to be at the bottom of the sort or Quantity will be in something like 35 PCS and I would want that to go on the bottom of its length sort.
    I am not sure how to do this through the sorting menu though. Is there perhaps a way I can sort them based on some expressions that decide where they should go? (I read on another forum post that reports only sort by the sorting/grouping ui and I am hoping there is a way around that.)

    This is what I would ideally like the results to be


    Length Quantity
    10 60
    10 200
    10 440
    10 560
    10 35 PCS
    6 180
    6 10 PCS
    5 430
    5 850
    56" 180
    45" 25 PCS


    I hope the example tables help. Thanks for any help!

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,790
    No doubt your values are strings (text data type). In text, 560 comes before 60. Best not to use text data type for numbers that you want to sort on or do math with. That means getting rid of " and PCS and adding fields for units of measure and quantity.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Micron View Post
    No doubt your values are strings (text data type). In text, 560 comes before 60. Best not to use text data type for numbers that you want to sort on or do math with. That means getting rid of " and PCS and adding fields for units of measure and quantity.
    Why does 560 come before 60? Shouldn't it come after since it is in the hundreds? I would understand that if it was sorting by the first 2 characters but I told it to sort by the entire value.

    Is there anyway that I can write a custom sort in vba?

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    As text it has no concept of the number value of the text, it's simply a string of characters.
    Consider these values as sorted alphabetically.

    Was
    We
    Where

    Just because "Where" is longer than "We" you wouldn't expect to appear before "We", would you?

    Similarly strings of numbers sorted ascending behave like this:
    String_id
    1
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    2
    20
    21
    22
    23
    24
    25
    26
    27
    3
    4
    5
    6
    7
    8
    9
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Do you want the inches to come before the feet?
    You could add a calculated field to the report's record source and use that for the sort order in the report design:

    SortOrder: IIf(InStr([Quantity],Chr(34))>0,CDbl("0." & Val([Quantity])),CDbl([Quantity]))

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,790
    Why does 560 come before 60?
    Because 5 comes before 6, the rest doesn't matter. Your data behaves like alpha because the values are strings (text data type). Think of it as being like a directory of telephone listings - strictly alphabetical. As I said, if you want numerical sorting you have to ditch the text characters like " and PCS and change the field data type to one of the number type options you have.

    EDIT
    the rest doesn't matter
    well, it does but only if you think of it in alphabetical terms.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Gicu View Post
    Do you want the inches to come before the feet?
    You could add a calculated field to the report's record source and use that for the sort order in the report design:

    SortOrder: IIf(InStr([Quantity],Chr(34))>0,CDbl("0." & Val([Quantity])),CDbl([Quantity]))

    Cheers,
    This did exactly what I wanted. Well I changed it to Lengths and then wrote one that searches for "PCS" in quantity and put it underneath.
    Thanks! I was unaware Val was a function.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Strange Behavior
    By GraeagleBill in forum Access
    Replies: 4
    Last Post: 08-13-2021, 04:01 PM
  2. Puzzling behavior
    By Dave14867 in forum Forms
    Replies: 23
    Last Post: 04-15-2019, 02:05 PM
  3. Ubnormal SUM behavior
    By spleewars in forum Forms
    Replies: 1
    Last Post: 04-10-2012, 10:27 AM
  4. Replies: 11
    Last Post: 01-12-2012, 07:55 PM
  5. Strange Behavior when Sorting
    By geniass in forum Queries
    Replies: 5
    Last Post: 09-02-2010, 03:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums