js array sorting

 

data

Data = the let [ 
        {chinese: ' Zeiss ' , Dictionary Dictionary English: ' Chase ', Score: 67 }, 
        {chinese: ' Allen ' , Dictionary Dictionary English: ' Allen ' , Score: 77}, 
{chinese: ' Zola ' , Dictionary Dictionary English : ' Zola ' , Score: 87},
{chinese: ' Baker ' , Dictionary Dictionary English: ' Baker ', Score: 99 },
{chinese:
' Berg ' , Dictionary Dictionary English: 'mountain', Score: 55 },
{chinese:
' Fitch ' , Dictionary Dictionary English: ' Fitch ', Score: 100 },
{chinese:
' Dean ' , Dictionary Dictionary English: ' Dean ', Score: 86 },
{chinese:
' Ecuador Er ' , Dictionary Dictionary English: ' of Earle ', Score: 40 },
{chinese:
' Henry ' , Dictionary Dictionary English: ' Henry ', Score: 70 },]

Characters ordering array property

  // The first alphabetical characters
     // Use the arrow functions
     // Note localeCompare] () is a built-in method js 
     data.sort ((A, B) => b.chinese.localeCompare (a.chinese, ' ZH ' )) ; // Z ~ A sorting
     data.sort((a, b)=> a.chinese.localeCompare(b.chinese, 'zh')); //a~z 排序
 

English sorting

 // The alphabetically
     // Use the arrow function 
     data.sort ((A, B) => b.english.charCodeAt ( 0 ) -a.english.charCodeAt ( 0 )); // Z ~ A sorting 
     data.sort ((A, B) => a.english.charCodeAt ( 0 ) -b.english.charCodeAt ( 0 )); // A ~ Z ordering

Number Sequencing

 // The digital sorting
     // Use the arrow function 
     data.sort ((A, B) => b.score-a.score); // score highest to lowest 
     data.sort ((a, b) = > a b.score-.score); // score from lowest to highest

 

Guess you like

Origin www.cnblogs.com/xuqp/p/11124297.html