当前位置:网站首页>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

原网站

版权声明
本文为[guangsu.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270550305036.html