当前位置:网站首页>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
边栏推荐
猜你喜欢
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
Tikv key performance parameters and optimization
Br backup test
YOLOv5解析 | 参数与性能指标
基于ESP32CAM实现WebSocket服务器实时点灯
New Taishan crowdfunding business diversion fission growth model in 2022
10 Honest Facts I Want To Share With All Junior Developers
Continuous management design
Related operations under Oracle Database
Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge
随机推荐
Evolution in the digital age
杭州网上开户是安全的吗?
上位机开发(固件下载软件之编码调试)
SDN basic overview
How to write an amazing design document?
通过函数seaborn.cubehelix_palette生成顺序调色板
ISIS的vsys(虚拟系统)
2022-06-12:在N*N的正方形棋盤中,有N*N個棋子,那麼每個格子正好可以擁有一個棋子。 但是現在有些棋子聚集到一個格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二維數組代錶,一
【云原生 | Kubernetes篇】Kubernetes 配置
15、 IO stream (I)
DM Experiment 6: filter replication
快速排序
面试必刷算法TOP101之单调栈 TOP31
New Taishan crowdfunding business diversion fission growth model in 2022
个人js学习笔记
Tree list under winfrom treelist related
Real time lighting of websocket server based on esp32cam
[Markov chain Monte Carlo] Markov chain Monte Carlo method sampling prior distribution
What is online blind box e-commerce? Can blind box e-commerce become a new bonus market for the Internet?
When the concept of industrial Internet just appeared, people only regarded it as a method to obtain b-end traffic