当前位置:网站首页>Sorting of numbers and strings
Sorting of numbers and strings
2022-06-13 07:07:00 【guangsu.】
Sorting numbers and strings
Introduction
We often encounter two sorting scenarios in our work .
- Yes
Numbers
Sort - Yes
character string
Sort ( Sort plain alphabetic strings | Sort pure numeric strings | Sort Chinese character strings | Sort strings mixed with alphanumeric special characters …)
This involves two different sorting algorithms
- Natural order / Natural ordering
- Dictionary order / Sort alphabetically
for example : In the natural sort algorithm , Numbers 2
Less than number 10
. But in computer sorting algorithms , character string 10
Less than string 2
, because 10
The first number in is less than 2
.
Dictionary order
Natural sequencing has nothing to say , Everyone will . Mainly about the dictionary preface .
In most languages , Both provide a way to compare the size of two strings , The comparison is actually the dictionary order of two strings .
Dictionary order dictionary order
, also called Alphabetic order alphabetical order
. The original meaning is to indicate the order of English words in the dictionary , In the field of computer, the size relationship extended into two arbitrary strings .
In the dictionary , Words are arranged in alphabetical order , such as alpha
stay beta
Before .
When the first letter is the same , Compare the order of the second letter of two words in the alphabet , such as account
stay advanced
Before , And so on .
The following words are arranged in dictionary order :
as
aster
astrolabe
astronomy
astrophysics
at
ataman
attack
baa
So look at the alphabetic order ( In fact, that is ASCII
surface )
123456789
ABCDEFG HIJKLMN OPQRST UVWXYZ
abcdefg hijklmn opqrst uvwxyz
Be careful :
- String sorting is determined by the first different letter encountered when two strings are traversed from left to right , Not by the length of the two strings .
- When Arabic numerals are sorted as numbers and letters , The results are different .
The practical application
Look at some practical problems
There are many authors of a book , You can often see such a sentence " Sort by author's last name strokes ".
There is a product that I need , Sort according to the first word of the organizational structure .
according to A-
The numbers after are sorted naturally
A-45
A-67
A-3
A-9
A-18
A-104
A-23
A-44
Rank by period number first , Those with the same number of periods are ranked by age .
Sort according to the code table of Chinese characters
mysql Some error prone sorting scenarios in
scene.1 When performing cardinality sorting on a combined string , It is better to split it into two fields to store
SELECT CONCAT(prefix, suffix) FROM items ORDER BY prefix , suffix;
SELECT item_no FROM items ORDER BY CAST(item_no AS UNSIGNED) , item_no
scene.2 Compare the efficiency of the two .
SELECT CONCAT(prefix, suffix) FROM items ORDER BY prefix , suffix;
SELECT CONCAT(prefix, suffix) as tmp FROM items ORDER BY tmp;
scene.3 Default value trap for sort order
SELECT prefix, suffix FROM items ORDER BY prefix , suffix DESC ;
Equivalent to : SELECT prefix, suffix FROM items ORDER BY prefix ASC, suffix DESC ;
It's not equal to : SELECT prefix, suffix FROM items ORDER BY prefix DESC, suffix DESC ;
Reference material
Chinese character sorting https://www.cnblogs.com/huahuahu/p/Unicode-zi-fu-chuan-pai-xu-gui-ze-yi-ru-he-que-din.html
mysql The sorting https://blog.csdn.net/q343509740/article/details/80611637
https://blog.csdn.net/qq_37050329/article/details/86637183#commentBox
边栏推荐
- Fundamentals of assembly language: register and addressing mode
- 个人js学习笔记
- Monotone stack top31 of interview must brush algorithm top101
- How to write an amazing design document?
- 怎么写出一份令人惊叹的设计文档?
- What is the new business model of Taishan crowdfunding in 2022?
- Tidb implementation plan -- I
- . Net code to implement get request and post request
- 检测循环数“142857“
- [turn to] FPGA interview questions
猜你喜欢
RT-Thread 模拟器 simulator LVGL控件:button 按钮事件
2022 - 06 - 12: dans un échiquier carré n * N, il y a n * n pièces, donc chaque pièce peut avoir exactement une pièce. Mais maintenant quelques pièces sont rassemblées sur une grille, par exemple: 2 0
基于ESP32CAM实现WebSocket服务器实时点灯
Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge
Why is the new e-commerce outlet mode so popular? What is the specific mode?
Differences between SQL and NoSQL of mongodb series
Br tool backup recovery
Yolov5 analysis | parameters and performance indicators
Implementation of fruit mall wholesale platform based on SSM
如何从头自己制作开发板?图文并茂,一步步操作给你看。
随机推荐
上位机开发(固件下载软件之编码调试)
Reflection of C # Foundation
基于ESP32CAM实现WebSocket服务器实时点灯
Fundamentals of assembly language: register and addressing mode
Vsys of Isis (virtual system)
Department store center supply chain management system
Ticdc introduction
在产业互联网的概念刚刚出现时,人们仅仅只是将其看成是一个获取B端流量的方法
Tidb execution plan -- II
RT thread simulator lvgl control: switch switch button control
AIO Introduction (VIII)
The new business outlet of beautiful Tiantian second mode will be popular in the Internet e-commerce market
RT-Thread 模拟器 simulator LVGL控件:switch 开关按钮控件
Personal JS learning notes
关于c#委托、事件相关问题
Xiaomi's one-sided interview questions (self sorting answers)
Can flush open a stock account? Is it safe?
Project analysis of Taishan crowdfunding mode: why is Taishan crowdfunding mode so popular?
Tidb implementation plan -- I
【马尔科夫链-蒙特卡罗】马尔科夫链-蒙特卡罗方法对先验分布进行抽样