Natural Order

Natural Order

  • it is also called version sorting

intro

  • 通常將字母和數字方開來處理,而數字的部分會以數字組的方式處理
  • 以避免下列狀況
    1
    2
    3
    4
    5
    6
    7
    // 按字母顺序排序:
    1. 1111
    2. 22

    // 自然排序:
    1. 22
    2. 1111

    code example

    1
    2
    3
    4
    5
    6
    SELECT 
    CONCAT(prefix, suffix)
    FROM
    items
    ORDER BY
    prefix , suffix;
    1
    2
    3
    4
    5
    SELECT 
    item_no
    FROM
    items
    ORDER BY CAST(item_no AS UNSIGNED) , item_no;
    1
    2
    3
    4
    5
    SELECT 
    item_no
    FROM
    items
    ORDER BY LENGTH(item_no) , item_no;

reference