Vue. js or js to achieve the sorting of Chinese A-Z, vue. jsjs Chinese a-z

Source: Internet
Author: User

Vue. js or js to achieve the sorting of Chinese A-Z, vue. jsjs Chinese a-z

According to the A-Z method, you can write in the vue's methods:

Methods: {pySort: function (arr, empty) {var $ this = this; if (! String. prototype. localeCompare) return null; var letters = "ABCDEFGHJKLMNOPQRSTWXYZ ". split (''); var zh =" ". split (''); var arrList = []; for (var m = 0; m <arr. length; m ++) {arrList. push (arr [m]. name);} var result = []; var curr; for (var I = 0; I <letters. length; I ++) {curr = {letter: letters [I], data: []}; if (I! = 26) {for (var j = 0; j <arrList. length; j ++) {var initial = arrList [j]. charAt (0); // capture the first character if (arrList [j]. charAt (0) = letters [I] | arrList [j]. charAt (0) = letters [I]. toLowerCase () {// the first character is English curr. data. push (arrList [j]);} else if (zh [I]! = '*' & $ This. isChinese (initial) {// determines whether there are no Chinese characters and whether it is a Chinese if (initial. localeCompare (zh [I])> = 0 &&(! Zh [I + 1] | initial. localeCompare (zh [I + 1]) <0) {// determines the category of the Chinese character, curr. data. push (arrList [j]) ;}}} else {for (var k = 0; k <arrList. length; k ++) {var ini = arrList [k]. charAt (0); // capture the first character if (! $ This. isChar (ini )&&! $ This. isChinese (ini) {curr. data. push (arrList [k]) ;}} if (empty | curr. data. length) {result. push (curr); // curr. data. sort (function (a, B) {// return B. localeCompare (a); // sorting; English sorting; Chinese characters are placed behind English; //}) ;}return result ;}, isChinese: function (temp) {var re =/[^ \ u4E00-\ u9FA5]/; if (re. test (temp) {return false;} return true;}, isChar: function (char) {var reg =/[A-Za-z]/; if (! Reg. test (char) {return false;} return true ;}}

Convert the json object obtained from php to an array, and call-> this. pySort (arr); js directly calls the pySort (arr) method to sort by A-Z format.

For example, the output format on the console:

Key Value A-Z in letter

Data: stores sorted arrays.

As for the vue to be placed on the page, use the v-for loop to v-for = "(key, value) in arr"

To obtain the value of the data array, continue to use v-for = "tmp in (key. data)" to loop through the elements you want to enter

This method is also common in js.

I hope this article will help you to sort data ~~~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.