JoshuaDavid
JoshuaDavid

Reputation: 9539

What is the precedence of characters when sorting in MySQL, PHP, or just in general?

Question: Where can I find the precedence of characters when sorting in MySQL, PHP, or just in general on Linux and Windows OS?

For example, everybody knows that a comes before b when performing an ascending sort on a string in MySQL. But what about other characters? Does the dollar-sign $ come before asterisk * ? Does a space come before an exclamation-mark? etc...

What dictates the sort order? Does it use underlying ascii / UTF-8 values? Is it different for different technologies?

Technologies to consider:

  1. Databases - MySQL / SQL / SQLite / Oracle / etc
  2. Programming languages (for string-sorting functions) - PHP / Javascript / ASP.NET / Visual C# / Python / Ruby / Objective C
  3. OS (i.e., sorting files by filename) - Windows / Linux / MacOS / iOS / Android

Upvotes: 5

Views: 1002

Answers (4)

c2h5oh
c2h5oh

Reputation: 4560

  1. Databases - collations, if no collation is set integer character value
  2. Programing languages - locale or integer character value or user defined function (usually all 3 options are available by default)
  3. OS - Locale

Upvotes: 1

Sherlock
Sherlock

Reputation: 7597

It gets even more interesting when considering other languages.

Ä and ö are letters with accents in Dutch and German, but 'real' letters in the Swedish alphabet (after Z, not between A and B/O and L respectively).

Upvotes: 1

Steve H.
Steve H.

Reputation: 6947

In database terms, this is called the collation sequence, and most character sets have a default. If you are using unicode, you might look here: http://www.unicode.org/reports/tr10/tr10-24.html

Upvotes: 1

tskuzzy
tskuzzy

Reputation: 36476

Characters are represented as mappings from an unsigned integer type to a particular glyph. The particular mapping is defined by the character set.

Thus when you compare characters, you are really comparing the integer that represents the character in a particular charset.

e.g. Java compares Strings based on their unicode representations. Source

Upvotes: 1

Related Questions