Character sorting method, system and device based on JavaScript and Unicode

文档序号:486951 发布日期:2022-01-04 浏览:24次 中文

阅读说明:本技术 一种基于JavaScript与Unicode的字符排序方法、系统及装置 (Character sorting method, system and device based on JavaScript and Unicode ) 是由 郝长征 李尊状 游鑫 崔凤 于 2021-09-30 设计创作,主要内容包括:本发明提出的一种基于JavaScript与Unicode的字符排序方法、系统及装置,所述包括:在JavaScript平台中创建一个排序规则函数,记为compare函数;在待排序的数组中提取相邻的两个元素,记为参数a和b;将参数a和b输入compare函数中;判断a与b是否相等,若相等则compare函数直接返回1;若不相等,进一步判断a是否有值,若a没有值,则compare函数直接返回1;若a有值,则判断b是否有值,若b没有值,则compare函数直接返回-1;若b有值,则使用预设的Unicode对比方法函数对a和b进行比较;compare函数根据比较结果生成返回值;根据返回值采用sort方法对待排序的数组进行对比排序。本发明能够有效提升复杂业务场景下,js处理字符串排序的整体性能。(The invention provides a character sorting method, a system and a device based on JavaScript and Unicode, wherein the method comprises the following steps: creating a sequencing rule function in the JavaScript platform and marking the sequencing rule function as a compare function; extracting two adjacent elements from an array to be sorted, and recording the two adjacent elements as parameters a and b; inputting the parameters a and b into a compare function; judging whether a is equal to b, and if so, directly returning the match function to 1; if the values are not equal, further judging whether a has a value, and if the value of a does not have a value, directly returning the match function to 1; if a has a value, judging whether b has a value, and if b has no value, directly returning the compare function to-1; if b has a value, comparing a and b by using a preset Unicode comparison method function; generating a return value according to the comparison result by the compare function; and performing comparison sorting on the arrays to be sorted by adopting a sort method according to the return value. The invention can effectively improve the overall performance of the js processing character string sequencing under the complex service scene.)

1. A character sorting method based on JavaScript and Unicode is characterized by comprising the following steps:

creating a sequencing rule function in the JavaScript platform and marking the sequencing rule function as a compare function;

extracting two adjacent elements from an array to be sorted, and recording the two adjacent elements as parameters a and b;

inputting the parameters a and b into a compare function;

judging whether a is equal to b, and if so, directly returning the match function to 1;

if the values are not equal, further judging whether a has a value, and if the value of a does not have a value, directly returning the match function to 1;

if a has a value, judging whether b has a value, and if b has no value, directly returning the compare function to-1;

if b has a value, comparing a and b by using a preset Unicode comparison method function;

generating a return value according to the comparison result by the compare function;

and performing comparison sorting on the arrays to be sorted by adopting a sort method according to the return value.

2. The JavaScript and Unicode-based character sorting method according to claim 1, wherein the comparing a and b using a preset Unicode comparison method function comprises:

converting the parameters a and b into corresponding Unicode codes;

judging whether a and b are both Chinese characters according to a Unicode encoding rule, if so, converting a and b into pinyin and obtaining a corresponding final contrast value; if not, judging whether a and b are both English characters according to the Unicode encoding rule;

if a and b are both English characters, converting a and b into English lower case characters according to Unicode coding to obtain corresponding final contrast values;

and comparing the final contrast value of a with the final contrast value of b, if the final contrast value of a is greater than the final contrast value of b, returning to 1, otherwise, returning to-1.

3. The JavaScript and Unicode-based character sorting method according to claim 2, wherein the converting the parameters a and b into corresponding Unicode codes specifically comprises:

the parameters a and b are converted to the corresponding Unicode codes using the charCodeAt () method.

4. The JavaScript and Unicode-based character sorting method according to claim 2, wherein the determining whether a and b are both chinese characters according to the Unicode encoding rule comprises:

looking at Unicode codes of a and b;

if the Unicode code values of a and b are both greater than 19968 and less than 40869, then a and b are both Chinese characters.

5. The JavaScript and Unicode based character sorting method according to claim 2, wherein the converting a and b into pinyin and obtaining the corresponding final contrast value comprises:

js plug-in is used for converting the Unicode coded values of a and b into Unicode codes corresponding to Pinyin characters after 19968 is subtracted respectively;

and (4) carrying out English lowercase conversion processing on the pinyin characters, and taking the processed Unicode encoding values of a and b as corresponding final comparison values.

6. The JavaScript and Unicode-based character sorting method according to claim 2, wherein the determining whether a and b are both english characters according to the Unicode encoding rule comprises:

judging whether a is a Chinese character and b is an English character according to the Unicode encoding rule, if so, returning to 1;

if not, judging whether a is an English character and b is a Chinese character according to the Unicode encoding rule, if so, returning to-1;

if not, a and b are both English characters.

7. A character sorting system based on JavaScript and Unicode, comprising:

the function creating unit is used for creating a sequencing rule function in the JavaScript platform and marking the sequencing rule function as a compare function;

the data extraction unit is used for extracting two adjacent elements from the array to be sorted and recording the two adjacent elements as parameters a and b;

the data injection unit is used for inputting the parameters a and b into a compare function;

the preliminary screening unit is used for judging whether the a and the b are equal, and if so, the match function directly returns to 1;

if the values are not equal, further judging whether a has a value, and if the value of a does not have a value, directly returning the match function to 1;

if a has a value, judging whether b has a value, and if b has no value, directly returning the compare function to-1;

the comparison unit is used for comparing a and b by using a preset Unicode comparison method function;

the return unit is used for generating a return value according to the comparison result through a compare function;

and the sorting unit is used for performing comparison sorting on the arrays to be sorted by adopting a sort method according to the return value.

8. A character sorting device based on JavaScript and Unicode is characterized by comprising the following components:

a memory for storing a computer program;

a processor for implementing the steps of the JavaScript and Unicode based character sorting method according to any one of claims 1 to 6 when executing the computer program.

Technical Field

The invention relates to the technical field of character data processing, in particular to a character sorting method, a system and a device based on JavaScript and Unicode.

Background

A sorting algorithm, i.e. one or more sets of data are reordered according to a given pattern by a specific algorithm factor. The new sequence follows a certain rule and embodies a certain rule, so that the processed data is convenient to screen and calculate, and the calculation efficiency is greatly improved. The most basic requirement for sorting is to have certain stability, that is, when two identical elements appear in a certain sequence at the same time, after a certain sorting algorithm, the relative positions of the two elements before and after sorting do not change. In other words, even two identical elements are each distinct in the ordering process and do not allow confusion.

Currently, common schemes for sorting in JavaScript platforms are bubble sorting, selection sorting, insertion sorting, and sort methods. The bubble sorting method has the disadvantages that the speed is low, the complexity in operation is O (n2), and the requirement of large array sorting cannot be met; the selection of the sorting is an unstable sorting algorithm, and the number of times of executing the comparison is too many, which easily causes unnecessary performance waste; the insertion sequence is similar to the insertion of playing cards, and once the sequence with a large order of magnitude is encountered, the operation times can increase in a geometric series manner; the sort method is a method of parsing through a browser engine, comparing and sorting are always performed with an ASCII value of a first character, and the method defaults to converting all elements into String first and then sorting, and a plurality of problems are generated when the method is directly used.

Therefore, how to realize the rapid and reasonable sequencing of character strings aiming at a large number of use scenes needing to sequence the character strings is an urgent problem to be solved.

Disclosure of Invention

Aiming at the problems in the prior art, the invention aims to provide a character sorting method, a system and a device based on JavaScript and Unicode, which can effectively improve the overall performance of js processing character string sorting in a complex service scene, avoid the defect of the sort function of js, and reasonably plan and sort character strings of different languages by adopting a Unicode numerical value positioning mode.

In order to achieve the purpose, the invention is realized by the following technical scheme:

a character sorting method based on JavaScript and Unicode comprises the following steps:

creating a sequencing rule function in the JavaScript platform and marking the sequencing rule function as a compare function;

extracting two adjacent elements from an array to be sorted, and recording the two adjacent elements as parameters a and b;

inputting the parameters a and b into a compare function;

judging whether a is equal to b, and if so, directly returning the match function to 1;

if the values are not equal, further judging whether a has a value, and if the value of a does not have a value, directly returning the match function to 1;

if a has a value, judging whether b has a value, and if b has no value, directly returning the compare function to-1;

if b has a value, comparing a and b by using a preset Unicode comparison method function;

generating a return value according to the comparison result by the compare function;

and performing comparison sorting on the arrays to be sorted by adopting a sort method according to the return value.

Further, the comparing a and b by using a preset Unicode comparison method function includes:

converting the parameters a and b into corresponding Unicode codes;

judging whether a and b are both Chinese characters according to a Unicode encoding rule, if so, converting a and b into pinyin and obtaining a corresponding final contrast value; if not, judging whether a and b are both English characters according to the Unicode encoding rule;

if a and b are both English characters, converting a and b into English lower case characters according to Unicode coding to obtain corresponding final contrast values;

and comparing the final contrast value of a with the final contrast value of b, if the final contrast value of a is greater than the final contrast value of b, returning to 1, otherwise, returning to-1.

Further, the converting the parameters a and b into corresponding Unicode codes specifically includes:

the parameters a and b are converted to the corresponding Unicode codes using the charCodeAt () method.

Further, the judging whether a and b are both Chinese characters according to the Unicode encoding rule includes:

looking at Unicode codes of a and b;

if the Unicode code values of a and b are both greater than 19968 and less than 40869, then a and b are both Chinese characters.

Further, the converting a and b into pinyin and obtaining corresponding final contrast value includes:

js plug-in is used for converting the Unicode coded values of a and b into Unicode codes corresponding to Pinyin characters after 19968 is subtracted respectively;

and (4) carrying out English lowercase conversion processing on the pinyin characters, and taking the processed Unicode encoding values of a and b as corresponding final comparison values.

Further, the judging whether a and b are both English characters according to the Unicode encoding rule includes: judging whether a is a Chinese character and b is an English character according to the Unicode encoding rule, if so, returning to 1;

if not, judging whether a is an English character and b is a Chinese character according to the Unicode encoding rule, if so, returning to-1;

if not, a and b are both English characters.

Correspondingly, the invention also discloses a character sorting system based on JavaScript and Unicode, which comprises:

the function creating unit is used for creating a sequencing rule function in the JavaScript platform and marking the sequencing rule function as a compare function;

the data extraction unit is used for extracting two adjacent elements from the array to be sorted and recording the two adjacent elements as parameters a and b; the data injection unit is used for inputting the parameters a and b into a compare function;

the preliminary screening unit is used for judging whether the a and the b are equal, and if so, the match function directly returns to 1;

if the values are not equal, further judging whether a has a value, and if the value of a does not have a value, directly returning the match function to 1;

if a has a value, judging whether b has a value, and if b has no value, directly returning the compare function to-1;

the comparison unit is used for comparing a and b by using a preset Unicode comparison method function;

the return unit is used for generating a return value according to the comparison result through a compare function;

and the sorting unit is used for performing comparison sorting on the arrays to be sorted by adopting a sort method according to the return value.

Correspondingly, the invention also discloses a character sorting device based on JavaScript and Unicode, which comprises:

a memory for storing a computer program;

a processor for implementing the character sorting method steps based on JavaScript and Unicode when the computer program is executed.

Compared with the prior art, the invention has the beneficial effects that: the invention provides a character sorting method, a system and a device based on JavaScript and Unicode. The match function design is carried out by adopting a Unicode encoding method different from the ASCII encoding method in the sort method, so that the usability of the sorting algorithm is more comprehensive, and the performance is improved to a certain extent.

The invention realizes that the front-end developer can quickly and reasonably carry out the character string sequencing. Aiming at a large number of use scenes needing to sort character strings, the method adopts a script-based sort method and Unicode conversion to support different service scenes, adopts a character conversion Unicode mode to perform sort on the services with high service complexity and time consumption by precisely positioning the Unicode position; for the services with low service complexity, the services can be sorted by adopting the most appropriate sorting algorithm through the analysis of a browser engine by utilizing the characteristics of the sort method. The invention not only solves the instability factor existing in the sort method, but also realizes the optimal solution scheme for different service scenes, and integrally improves the performance of the project in the sequencing of the operation complex service scenes.

Therefore, compared with the prior art, the invention has prominent substantive features and remarkable progress, and the beneficial effects of the implementation are also obvious.

Drawings

In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the provided drawings without creative efforts.

FIG. 1 is a process flow diagram of an embodiment of the present invention.

FIG. 2 is a flow chart of a method for comparing a and b using a predetermined Unicode comparison method function according to an embodiment of the present invention.

FIG. 3 is a system block diagram of an embodiment of the present invention.

In the figure, 1 is a function creating unit, 2 is a data extracting unit, 3 is a data injecting unit, 4 is a preliminary screening unit, 5 is a comparing unit, 6 is a returning unit, and 7 is a sorting unit.

Detailed Description

The following description of the embodiments of the present invention will be made with reference to the accompanying drawings.

As shown in fig. 1, a method for sorting characters based on JavaScript and Unicode includes:

firstly, a sorting rule function is created in a JavaScript platform and is marked as a compare function. Then, two adjacent elements are extracted from the array to be sorted, and are marked as parameters a and b, and the parameters a and b are input into a compare function.

In the match function, firstly, judging whether a is equal to b, and if so, directly returning the match function to 1; if the values are not equal, further judging whether a has a value, and if the value of a does not have a value, directly returning the match function to 1; if a has a value, judging whether b has a value, and if b has no value, directly returning the compare function to-1.

If b has a value, comparing a and b by using a preset Unicode comparison method function, and generating a return value by the compare function according to the comparison result.

And finally, performing comparison sorting on the arrays to be sorted by adopting a sort method according to the return value.

On the basis of the above method, as shown in fig. 2, comparing a and b by using a preset Unicode comparison method function includes:

first, parameters a and b are converted into corresponding Unicode encodings using the charCodeAt () method.

And then, judging whether a and b are both Chinese characters according to the Unicode encoding rule, specifically, if the Unicode encoding values of a and b are both greater than 19968 and less than 40869, then a and b are both Chinese characters.

If a and b are both Chinese characters, converting a and b into pinyin and obtaining corresponding final contrast value. Using a Pinyin-by-chicken.js plug-in, converting the Unicode coding values of a and b into Unicode codes corresponding to Pinyin characters after subtracting 19968 respectively; and performing English lowercase conversion processing on the pinyin characters, and taking the processed Unicode encoding values of a and b as corresponding final comparison values.

If a and b are not the same as Chinese characters, judging whether a and b are the same as English characters according to the Unicode coding rule. The method specifically comprises the following steps: judging whether a is a Chinese character and b is an English character according to the Unicode encoding rule, if so, returning to 1; if not, judging whether a is an English character and b is a Chinese character according to the Unicode encoding rule, if so, returning to-1; if not, a and b are both English characters.

And under the condition that whether the a and the b are both English characters, converting the a and the b into English lower case characters according to the Unicode to obtain corresponding final contrast values.

And finally, comparing the final contrast value of a with the final contrast value of b, if the final contrast value of a is greater than the final contrast value of b, returning to 1, otherwise, returning to-1.

The invention provides a character sorting method based on JavaScript and Unicode, which effectively improves the overall performance of sorting strings processed by js in a complex service scene, avoids the defect of the sort function of the js, and reasonably plans and sorts the strings of different languages by adopting a mode of positioning Unicode numerical values. A large number of performance experiments and tests are performed under various application scenes such as list front-end sequencing, file sequencing and the like, and a remarkable effect can be achieved on the improvement of the whole page loading speed and the data processing speed.

Correspondingly, as shown in fig. 3, the present invention also discloses a character sorting system based on JavaScript and Unicode, including:

the function creating unit 1 is configured to create a sorting rule function in the JavaScript platform, and mark the sorting rule function as a compare function.

And the data extraction unit 2 is used for extracting two adjacent elements in the array to be sorted, and the two adjacent elements are marked as parameters a and b.

And the data injection unit 3 is used for inputting the parameters a and b into the compare function.

The preliminary screening unit 4 is used for judging whether a is equal to b or not, and if so, the match function directly returns to 1; if the values are not equal, further judging whether a has a value, and if the value of a does not have a value, directly returning the match function to 1; if a has a value, judging whether b has a value, and if b has no value, directly returning the compare function to-1.

And the comparison unit 5 is used for comparing a and b by using a preset Unicode comparison method function.

And the return unit 6 is used for generating a return value according to the comparison result through a compare function.

And the sorting unit 7 is used for performing comparison sorting on the arrays to be sorted by adopting a sort method according to the return value.

Correspondingly, the invention also discloses a character sorting device based on JavaScript and Unicode, which comprises:

a memory for storing a computer program;

a processor for implementing the character sorting method steps based on JavaScript and Unicode when the computer program is executed.

Those skilled in the art will readily appreciate that the techniques of the embodiments of the present invention may be implemented as software plus a required general purpose hardware platform. Based on such understanding, the technical solutions in the embodiments of the present invention may be embodied in the form of a software product, where the computer software product is stored in a storage medium, such as a usb disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and the like, and the storage medium can store program codes, and includes instructions for enabling a computer terminal (which may be a personal computer, a server, or a second terminal, a network terminal, and the like) to perform all or part of the steps of the method in the embodiments of the present invention. The same and similar parts in the various embodiments in this specification may be referred to each other. Especially, for the terminal embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant points can be referred to the description in the method embodiment.

In the embodiments provided by the present invention, it should be understood that the disclosed system, system and method can be implemented in other ways. For example, the above-described system embodiments are merely illustrative, and for example, the division of the units is only one logical functional division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, systems or units, and may be in an electrical, mechanical or other form.

The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.

In addition, functional modules in the embodiments of the present invention may be integrated into one processing unit, or each module may exist alone physically, or two or more modules are integrated into one unit.

Similarly, each processing unit in the embodiments of the present invention may be integrated into one functional module, or each processing unit may exist physically, or two or more processing units are integrated into one functional module.

The invention is further described with reference to the accompanying drawings and specific embodiments. It should be understood that these examples are for illustrative purposes only and are not intended to limit the scope of the present invention. Further, it should be understood that various changes or modifications of the present invention may be made by those skilled in the art after reading the teaching of the present invention, and these equivalents also fall within the scope of the present application.

11页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种基于XML的协议动态组包和解析方法

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!