In the previous article, I introduced the JavaScript sort array sort method and self-implemented sort method to you, and used my own method to implement the sorting of numeric arrays.
Of course, in actual use, it is more convenient for me to use the sort method. However, in my previous blog post, I only implemented numeric sorting, while the srot method can sort letters by default! My code can only sort numbers, and it still looks weak.
So, I have to add a sorting method that can sort letters or even Chinese.
Implement code
$(function(){var arr = ["Jack","Book","Fung",76,"Love","Mark","China","china","phone","Andy Lau"];console.log('original array');console.log('arr);console.log('for method sorted from small to large');console.log('arrSortMinToMax(arr));console.log('for method sorted from large to small');console.log('arrSortMaxToMin(arr));console.log('original array');console.log('arr);});function arrMinNum(arr){var minNum = Infinity, index = -1,minVul = "";for (var i = 0; i < arr.length; i++) {if (typeof(arr[i]) == "string") {if (arr[i].charCodeAt()<minNum) {minNum = arr[i].charCodeAt();minVul = arr[i];index = i;}}else {if (arr[i]<minNum) {minNum = arr[i];minVul = arr[i]index = i;}}};return {"minNum":minVul,"index":index};}function arrSortMinToMax(arr){var arrNew = [];var arrOld = arr.concat();for (var i = 0; i < arr.length; i++) {arrNew.push(arrMinNum(arrOld).minNum);arrOld.splice(arrMinNum(arrOld).index,1)};return (arrNew);}function arrMaxNum(arr){var maxNum = -Infinity, index = -1,maxVul = "";for (var i = 0; i < arr.length; i++) {if (typeof(arr[i]) == "string") {if (arr[i].charCodeAt()>maxNum) {maxNum = arr[i].charCodeAt();maxVul = arr[i];index = i;}}else {if (arr[i]>maxNum) {maxNum = arr[i];maxVul = arr[i];index = i;}}};return {"maxNum":maxVul,"index":index};}function arrSortMaxToMin(arr){var arrNew = [];var arrOld = arr.slice(0);for (var i = 0; i < arr.length; i++) {arrNew.push(arrMaxNum(arrOld).maxNum);arrOld.splice(arrMaxNum(arrOld).index,1);};return (arrNew);}The running screenshot is as follows:
Sorting principle
If it is a number, it is directly compared with the number.
If it is a string, it is converted to Unicode encoding using charCodeAt() for sorting.
Unicode is an integer between 0 - 65535
Other Instructions
According to the normal sorting logic, it should be: numbers are smaller than all letters, letters are smaller than all Chinese, and Chinese should be sorted according to the first letter of the pinyin of the first character.
My code has not been implemented except that the letters are smaller than all Chinese.
Logic should also be realized, find out the Chinese characters in numbers, compare numbers with arrays, compare letters with letters, compare Chinese with Chinese, and then splice arrays
It may be a little more troublesome to get the initials of the first characters in Chinese.
Chinese characters can be compared directly.
As shown in the picture above, it makes sense for Zhang Fei to be the boss. JavaScript finally made Zhang Fei famous a thousand years later. He should have been the boss back then! ~