Method for realizing data multi-field sorting based on JavaScript
By creating a sorted field array in JavaScript and sorting it according to type and order, the problem of data type mismatch in multi-field sorting is solved, and multi-field sorting with high accuracy and flexibility is achieved, improving user experience and performance.
Patent Information
- Application Number
- CN202411112648.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-14
- Publication Date
- 2024-12-31
AI Technical Summary
The prior art cannot meet the user's multiple-group field viewing needs when processing multi-field sorting, resulting in the user experience being affected and prone to sorting errors or confusion caused by mismatch in data types.
By implementing the data multi-field sorting method in JavaScript, creating a sorted field array and sorting it according to the field type and order, automatically processing null values and special values, adopting type weight proportional sorting, and supporting pagination parameters to improve sorting flexibility and performance.
It realizes the accuracy and accuracy of sorting, provides high flexibility and high performance multi-field sorting, can correctly handle special values, and improves data processing speed and user experience.
Smart Images

Figure CN119225684A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and in particular to a method for implementing multi-field data sorting based on JavaScript. Background Art
[0002] In a large amount of data, users have various multi-field combination sorting requirements. When multiple groups are involved, each time you want to view by multiple groups of fields, you can only select a single field to sort and then re-sort; for example, if you want to sort by province, then look at the data sorting of each city in the same province, it cannot meet user needs, affecting user experience.
[0003] JavaScript, abbreviated as JS, is a high-level, multi-paradigm, interpreted programming language. It is a prototype-based, function-first language that supports object-oriented programming, imperative programming, and functional programming. Summary of the invention
[0004] In view of the problems existing in the prior art, the present invention provides a method for implementing multi-field data sorting based on JavaScript, which avoids sorting errors caused by data type mismatch by accurately processing fields of different data types. It can also automatically process special cases such as null values and missing values, avoiding sorting errors or confusions that may exist in traditional sorting methods, and ensuring the integrity and accuracy of the sorting results.
[0005] The technical solution of the present invention is: a method for implementing multi-field data sorting based on JavaScript, comprising the following steps:
[0006] S1. Receive a single or mixed sorting request for original data including Chinese, English, numbers, version numbers, dates, intervals, '--', null, 'null', '(null)', where the sorting request carries the original data and a sorting field array;
[0007] The sorting field array refers to an array of objects arranged in the order of the required sorting fields; the sorting fields of the original data and the field sorting order are specified in the object array; the sorting field array is created for the sorting fields according to the field sorting order of the sorting field array carried in the sorting request; the sorting field array refers to the fields and field sorting order that need to be sorted extracted from the object array;
[0008] S2, sorting the fields according to the order of arrangement of the sorting field array described in step S1;
[0009] S3. Sort the field data according to the order of arrangement of the object array in the sorting field array obtained in step S1 and the sorting order of the field obtained in step S2.
[0010] Furthermore, the step S3 is specifically as follows:
[0011] S31, using the original data, starting from the first data, perform a pairwise comparison based on the values of the field data; if the values of the two data are completely equal, repeat step S3 for the two data and enter the next sorting field for sorting; if the values of the two data are not equal, sort the field data according to the sorting order of the field;
[0012] S32. All data are compared in pairs and sorted in sequence according to the fields to obtain a successfully sorted array result.
[0013] Furthermore, the step S31 is specifically as follows:
[0014] S311, data contains null, 'null', '(null)', sorted by type weight ratio;
[0015] S312: The data completely conforms to the date type, and the field data is sorted according to the sorting order of the field;
[0016] S313, the data completely conforms to the interval type, and the field data is sorted according to the sorting order of the field;
[0017] S314, the data completely conforms to the numeric type, and the field data is sorted according to the sorting order of the field;
[0018] S315: The data completely complies with the version type, and the field data is sorted according to the sorting order of the field;
[0019] S316, the data completely conforms to the Chinese type, and the data of the field is sorted according to the sorting order of the field;
[0020] S317, the data is completely in English type, and the data in the field is sorted according to the sorting order of the field;
[0021] S318. Data belongs to different types and is sorted according to the type weight ratio.
[0022] Furthermore, the type weight ratio ranking is specifically as follows: a custom configuration may be carried in the sorting request in step S1 according to business needs; if not carried, the default configuration is used; the smaller the value, the higher the weight and the higher the ranking; the default configuration is as follows:
[0023] Number: 1;
[0024] Chinese: 2;
[0025] English: 3;
[0026] Date: 4;
[0027] Range: 5;
[0028] Version number: 6;
[0029] Others: 99;
[0030] '--': 100;
[0031] 'null': 101;
[0032] '(null)':102.
[0033] Furthermore, paging parameters can be carried in the sorting request in step S1 according to business needs. The paging parameters include the current page and the number of entries. If paging parameters are carried, the array results are intercepted using the current page and the number of entries and the final sorting result is returned. If no paging parameters are carried, the successfully sorted array results are directly returned.
[0034] Beneficial effects of the present invention:
[0035] 1. Accurate sorting: By accurately processing fields of different data types, sorting errors caused by data type mismatch are avoided. It can also automatically handle special cases such as null values and missing values, avoiding sorting errors or confusions that may exist in traditional sorting methods, and ensuring the integrity and accuracy of the sorting results.
[0036] 2. High sorting flexibility: The present invention allows users to freely combine and configure sorting fields according to actual needs, including field priority, sorting direction (ascending or descending), etc., providing extremely high sorting flexibility. This flexibility enables users to customize sorting schemes that meet their needs according to different business needs and preferences.
[0037] 3. High performance: The present invention reduces data redundancy and repeated calculations in the sorting process by creating a sorting field array and backing up the field data to be sorted. This optimization strategy enables the present invention to maintain a high performance level when processing large amounts of data, reduce sorting time, and increase data processing speed.
[0038] 4. Easy to integrate and maintain: The present invention is based on JavaScript, has good cross-platform and compatibility, and can be easily integrated into various front-end frameworks and systems. At the same time, its concise code structure and clear logic design also make maintenance and expansion relatively easy.
[0039] 5. Enhanced data visualization: Through mixed sorting, users can more intuitively understand the distribution of data in different dimensions, which helps to quickly identify patterns and trends in the data.
[0040] 6. Strong ability to handle special values: The present invention can correctly handle various data including special values such as '--', null, 'null', '(null)', and avoid problems such as sorting errors or program crashes caused by special values. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Figure 1 A schematic diagram of a flow chart of a data sorting method provided by an embodiment of the present invention;
[0042] Figure 2 The figure shows the final sorting result of the ascending multi-field sorting of the stage summary of the same visitor in the same province and the same city processed by the embodiment of the present invention. DETAILED DESCRIPTION
[0043] The present invention will be further described below in conjunction with the accompanying drawings.
[0044] If the user needs to view the same device brand in the same province and city, sort them according to event time, operating system version, and cumulative recharge times. Figure 1 , 2 And embodiments of the present invention are provided to illustrate the technical solution of the present invention in detail.
[0045] Step 1: Receive a single or mixed sorting request for the original data including Chinese, English, numbers, version numbers, dates, intervals, '--', null, 'null', '(null)', and other data, wherein the sorting request carries the original data and a sorting array; wherein the sorting array arranges the object array in the order of the required sorting fields; wherein the object array specifies the sorting fields of the original data and the field sorting order. According to the order of the sorting array carried in the sorting request, a sorting field array is created for the sorting field.
[0046] In this step, it should be noted that the original data refers to the data containing the fields to be sorted, such as province, city, device brand, event time, operating system version, cumulative number of recharges, etc. The sorting array is composed according to the user operation. The sorting field array is obtained by traversing the sorting array and taking the field string. For example, the sorting field array of the embodiment of the invention is composed of province, city, device brand, event time, operating system version, and cumulative number of recharges.
[0047] In an embodiment of the present invention, the sorting request specifies the sorting fields of the original data, such as the province field, city field, device brand field, event time field, operating system version field, cumulative recharge number field, etc. of the original data; the sorting array arranges the object array in the order of the required sorting fields, such as the sorting array is composed of province in descending order, city in descending order, device brand in ascending order, event time in descending order, operating system version in descending order, and cumulative recharge number in descending order as the object array.
[0048] Step 2: Sort the field data according to the order of the sort field array and the sort order of the field.
[0049] Step 2 includes the following sub-steps 2-1 to 2-2:
[0050] Step 2-1: Use the original data to compare two records based on the value of the field starting from the first record. If the values of the two records are completely equal, repeat step 3 to enter the next sorting field for sorting;
[0051] Step 2-2: If the values of the two data are not equal, sort the field data according to the sorting order of the field.
[0052] In the sorting step, the field data is sorted as follows:
[0053] The data contains null, 'null', and '(null)', and is sorted according to the type weight ratio described in step S2;
[0054] The data fully conforms to the date type, and the data in this field is sorted according to the sorting order of this field;
[0055] The data completely conforms to the interval type, and the field data is sorted according to the sorting order of the field;
[0056] The data completely conforms to the numeric type, and the data in this field is sorted according to the sorting order of this field;
[0057] The data fully complies with the version type, and the data in this field is sorted according to the sorting order of this field;
[0058] The data fully complies with the Chinese type, and the data in this field is sorted according to the sorting order of this field;
[0059] The data is completely in English type, and the data in this field is sorted according to the sorting order of this field;
[0060] The data belongs to different types and is sorted according to the type weight ratio described in step S2.
[0061] In the embodiment of the present invention, as shown in the attached specification Figure 2, the province field data conforms to mixed sorting, sorted according to mixed descending order, the weight of '--' is 100, the weight of Chinese is 2, and in descending order, '--' is ranked before Chinese. After sorting is completed, recursively enter the next field. The city field conforms to the Chinese type. According to the previous order, the data with equal values of the province field is arranged as a group of city fields. Sorted in descending order according to Chinese. After sorting is completed, recursively enter the next field. The device brand field data conforms to mixed sorting. According to the previous order, the data with equal values of the city field is arranged as a group of device brand fields. Sorted according to mixed ascending order, the weight of Chinese is 2, and the weight of English is 3. In ascending order, Chinese is ranked before English. After sorting is completed, recursively enter the next field. The event time field conforms to the time type. According to the previous order, the data with equal values of the device brand field is arranged as a group of event time fields. Sorted in descending order according to Chinese. After sorting is completed, recursively enter the next field. The operating system version field conforms to the version type. The data with the same value of the previous order field event time are arranged as a group. The operating system version field is sorted in descending order according to the version; if the previous order field does not have the same value, the sorting result of the previous order field is maintained; after the sorting is completed, the next field is recursively entered. The cumulative recharge times field conforms to the interval type. The data with the same value of the previous order field operating system version is arranged as a group. The cumulative recharge times field is sorted in descending order according to the cumulative recharge times; if the previous order field does not have the same value, the sorting result of the previous order field is maintained.
[0062] Step 3: Compare and sort all the data in pairs to get the sorted array result. According to the paging parameters, intercept the data of the current page and display the data on the front end.
[0063] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A method for implementing multi-field data sorting based on JavaScript, characterized in that: The following steps are involved: S1. Receive a single or mixed sorting request for original data including Chinese, English, numbers, version numbers, dates, intervals, '--', null, 'null', '(null)', where the sorting request carries the original data and a sorting field array; The sorting field array refers to an array of objects arranged in the order of the required sorting fields; the sorting fields of the original data and the field sorting order are specified in the object array; the sorting field array is created for the sorting fields according to the field sorting order of the sorting field array carried in the sorting request; the sorting field array refers to the fields and field sorting order that need to be sorted extracted from the object array; S2, sorting the fields according to the order of arrangement of the sorting field array described in step S1; S3. Sort the field data according to the order of arrangement of the object array in the sorting field array obtained in step S1 and the sorting order of the field obtained in step S2.
2. According to the method for implementing multi-field data sorting based on JavaScript in claim 1, it is characterized in that: The step S3 is specifically as follows: S31, using the original data, starting from the first data, perform a pairwise comparison based on the values of the field data; if the values of the two data are completely equal, repeat step S3 for the two data and enter the next sorting field for sorting; if the values of the two data are not equal, sort the field data according to the sorting order of the field; S32. All data are compared in pairs and sorted in sequence according to the fields to obtain a successfully sorted array result.
3. According to the method for implementing multi-field data sorting based on JavaScript in claim 2, it is characterized in that: The step S31 is specifically as follows: S311, data contains null, 'null', '(null)', sorted by type weight ratio; S312: The data completely conforms to the date type, and the field data is sorted according to the sorting order of the field; S313, the data completely conforms to the interval type, and the field data is sorted according to the sorting order of the field; S314, the data completely conforms to the numeric type, and the field data is sorted according to the sorting order of the field; S315: The data completely complies with the version type, and the field data is sorted according to the sorting order of the field; S316, the data completely conforms to the Chinese type, and the data of the field is sorted according to the sorting order of the field; S317, the data is completely in English type, and the data in the field is sorted according to the sorting order of the field; S318. Data belongs to different types and is sorted according to the type weight ratio.
4. According to the method for implementing multi-field data sorting based on JavaScript in claim 3, it is characterized in that: The type weight ratio ranking is specifically as follows: a custom configuration may be carried in the sorting request in step S1 according to business needs; if not carried, the default configuration is used; the smaller the value, the higher the weight and the higher the ranking; the default configuration is as follows: Number: 1; Chinese: 2; English: 3; Date: 4; Range: 5; Version number: 6; Others: 99; '--':100; 'null': 101; '(null)':
102.
5. According to the method for implementing multi-field data sorting based on JavaScript in claim 1, it is characterized in that: Paging parameters can be carried in the sorting request in step S1 according to business needs. The paging parameters include the current page and the number of entries. If paging parameters are carried, the array result is intercepted using the current page and the number of entries and the final sorting result is returned. If paging parameters are not carried, the successfully sorted array result is directly returned.
Citation Information
Cited By
Database data processing method and device based on multi-rule dynamic sorting
CN120470157A
