Character Sort Order

jdpjamesp

ProgressTalk.com Moderator
Staff member
More of a question of 'why?' rather than a solution requirement. I've just been looking at the sort order of character fields in a by statement due to some rather unexpected results. So I wrote the code

Code:
DEFINE TEMP-TABLE ttTest NO-UNDO 
  FIELD Test AS CHAR
  FIELD ChrNum AS INT. 

DEFINE VARIABLE lvi AS INTEGER     NO-UNDO.


DO  lvi = 1 TO 255:
  CREATE ttTest. 
  ASSIGN 
    ttTest.Test = CHR(lvi)
    ttTest.ChrNum = lvi.
END.

OUTPUT TO c:\temp\sortorder.csv.
FOR EACH ttTest
  BY Test:
  EXPORT DELIMITER "," ttTest.
END.
OUTPUT CLOSE.
This produced the attached file. Can anyone explain to me why the sort order is like this? There doesn't seem to be any rhyme nor reason to it in a lot of circumstances. Why, for example would ÿ come before y?
 
I don't know why but it has something to do with the codepage setup.
I have run the same program on a iso8859-15 and you can see the result in the attached file.
 

Attachments

Yes it is caused by the chosen codepage at startup: http://en.wikipedia.org/wiki/Code_page (for a detailed description). Anyway if you use the correct codepage for the language, then the queries for strings stored in db using the language that is with the same codepage will give you the correct order.
 
Thanks folks - that's exactly what I wanted. Makes sense I guess. It just had me flummoxed!
 
Back
Top