導航:首頁 > 編程語言 > js實現字母排序demo

js實現字母排序demo

發布時間:2023-01-29 18:12:46

js按照開頭首字母排序

functionstringSort(str){
if(typeofstr==='string')
str=str.split(',');
if(!Array.isArray(str)){
console.error('參數復類型錯制誤,必須為數組或以(,)分割的字元串.')
returnstr;
}
str.sort();
returnstr.join();
}
//測試
vartest1=["BB000","AB621","C752B","AC700","D5657"];
console.log(stringSort(test1));
vartest2="BB000,AB621,C752B,AC700,D5657";
console.log(stringSort(test2));

Ⅱ JS,實現表格內容排序

<! HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>main.html</title>
<style type="text/css">
table {
width: 300px; border : 1px solid black;
border-collapse: collapse;
border: 1px solid black;
}

td {
border: 1px solid black;
}
</style>
<script type="text/javascript">
function paiXu ()
{
var tabNode = document.getElementsByTagName ("table")[0];
var trs = tabNode.rows;
var arr = new Array;
for ( var x = 0; x < trs.length; x++)
{
arr.push (trs[x]);
}
sortt (arr);
for ( var x = 0; x < arr.length; x++)
{
tabNode.tBodies[0].appendChild (arr[x]);
}
}

function sortt (arr)
{
for ( var x = 0; x < arr.length; x++)
{
for ( var y = x + 1; y < arr.length; y++)
{
if (parseInt (arr[x].cells[1].childNodes[0].nodeValue) <= parseInt (arr[y].cells[1].childNodes[0].nodeValue))
{
var tmp = arr[x];
arr[x] = arr[y];
arr[y] = tmp;
}
}
}
}
</script>
</head>

<body>
<input type="button" value="排序" onclick="paiXu()">
<table>
<tr>
<td>小明</td>
<td>23</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小花</td>
<td>21</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小高</td>
<td>12</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小林</td>
<td>25</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小王</td>
<td>35</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>

Ⅲ 用JS實現排序的功能

js常用排序實現,參考代碼如下

<script>
Array.prototype.swap=function(i,j)
{
vartemp=this[i];
this[i]=this[j];
this[j]=temp;
}

Array.prototype.bubbleSort=function()
{
for(vari=this.length-1;i>0;--i)
{
for(varj=0;j<i;++j)
{
if(this[j]>this[j+1])this.swap(j,j+1);
}
}
}

Array.prototype.selectionSort=function()
{
for(vari=0;i<this.length;++i)
{
varindex=i;
for(varj=i+1;j<this.length;++j)
{
if(this[j]<this[index])index=j;
}
this.swap(i,index);
}
}

Array.prototype.insertionSort=function()
{
for(vari=1;i<this.length;++i)
{
varj=i,value=this[i];
while(j>0&&this[j-1]>value)
{
this[j]=this[j-1];
--j;
}
this[j]=value;
}
}

Array.prototype.shellSort=function()
{
for(varstep=this.length>>1;step>0;step>>=1)
{
for(vari=0;i<step;++i)
{
for(varj=i+step;j<this.length;j+=step)
{
vark=j,value=this[j];
while(k>=step&&this[k-step]>value)
{
this[k]=this[k-step];
k-=step;
}
this[k]=value;
}
}
}
}

Array.prototype.quickSort=function(s,e)
{
if(s==null)s=0;
if(e==null)e=this.length-1;
if(s>=e)return;
this.swap((s+e)>>1,e);
varindex=s-1;
for(vari=s;i<=e;++i)
{
if(this[i]<=this[e])this.swap(i,++index);
}
this.quickSort(s,index-1);
this.quickSort(index+1,e);
}

Array.prototype.stackQuickSort=function()
{
varstack=[0,this.length-1];
while(stack.length>0)
{
vare=stack.pop(),s=stack.pop();
if(s>=e)continue;
this.swap((s+e)>>1,e);
varindex=s-1;
for(vari=s;i<=e;++i)
{
if(this[i]<=this[e])this.swap(i,++index);
}
stack.push(s,index-1,index+1,e);
}
}

Array.prototype.mergeSort=function(s,e,b)
{
if(s==null)s=0;
if(e==null)e=this.length-1;
if(b==null)b=newArray(this.length);
if(s>=e)return;
varm=(s+e)>>1;
this.mergeSort(s,m,b);
this.mergeSort(m+1,e,b);
for(vari=s,j=s,k=m+1;i<=e;++i)
{
b[i]=this[(k>e||j<=m&&this[j]<this[k])?j++:k++];
}
for(vari=s;i<=e;++i)this[i]=b[i];
}

Array.prototype.heapSort=function()
{
for(vari=1;i<this.length;++i)
{
for(varj=i,k=(j-1)>>1;k>=0;j=k,k=(k-1)>>1)
{
if(this[k]>=this[j])break;
this.swap(j,k);
}
}
for(vari=this.length-1;i>0;--i)
{
this.swap(0,i);
for(varj=0,k=(j+1)<<1;k<=i;j=k,k=(k+1)<<1)
{
if(k==i||this[k]<this[k-1])--k;
if(this[k]<=this[j])break;
this.swap(j,k);
}
}
}

functiongenerate()
{
varmax=parseInt(txtMax.value),count=parseInt(txtCount.value);
if(isNaN(max)||isNaN(count))
{
alert("個數和最大值必須是一個整數");
return;
}
vararray=[];
for(vari=0;i<count;++i)array.push(Math.round(Math.random()*max));
txtInput.value=array.join(" ");
txtOutput.value="";
}

functiondemo(type)
{
vararray=txtInput.value==""?[]:txtInput.value.replace().split(" ");
for(vari=0;i<array.length;++i)array[i]=parseInt(array[i]);
vart1=newDate();
eval("array."+type+"Sort()");
vart2=newDate();
lblTime.innerText=t2.valueOf()-t1.valueOf();
txtOutput.value=array.join(" ");
}
</script>

<bodyonload=generate()>
<tablestyle="width:100%;height:100%;font-size:12px;font-family:宋體">
<tr>
<tdalign=right>
<textareaid=txtInputreadonlystyle="width:100px;height:100%"></textarea>
</td>
<tdwidth=150align=center>
隨機數個數<inputid=txtCountvalue=500style="width:50px"><br><br>
最大隨機數<inputid=txtMaxvalue=1000style="width:50px"><br><br>
<buttononclick=generate()>重新生成</button><br><br><br><br>
耗時(毫秒):<labelid=lblTime></label><br><br><br><br>
<buttononclick=demo("bubble")>冒泡排序</button><br><br>
<buttononclick=demo("selection")>選擇排序</button><br><br>
<buttononclick=demo("insertion")>插入排序</button><br><br>
<buttononclick=demo("shell")>謝爾排序</button><br><br>
<buttononclick=demo("quick")>快速排序(遞歸)</button><br><br>
<buttononclick=demo("stackQuick")>快速排序(堆棧)</button><br><br>
<buttononclick=demo("merge")>歸並排序</button><br><br>
<buttononclick=demo("heap")>堆排序</button><br><br>
</td>
<tdalign=left>
<textareaid=txtOutputreadonlystyle="width:100px;height:100%"></textarea>
</td>
</tr>
</table>
</body>

Ⅳ 字母排序js sort

先將字母塞到數組里,然後調用sort方法便好。
let arr = ['c','a', 'b']
arr.sort()

Ⅳ jquery或javascipte實現標簽內文字按拼音首字母排序

jq:

varwrap=$('div');

varhtml=
wrap.find('a')
.map(function(){//獲取文字
return$.trim($(this).text())
})
.toArray()//轉換jq為數組
.sort(function(a,b){
returna.localeCompare(b)//按照順序排序
})
.map(function(txt){//拼接html
return'<a>'+txt+'</a>';
})
.join('');

wrap.html(html);//輸出


js:
functiontoArray(nodes){
return[].slice.call(nodes,0);
}

varwrap=document.querySelector('div');
varlinks=wrap.querySelectorAll('a');
varhtml=
toArray(links)
.map(function(el,i){//讀取文字
return(el.innerText||el.textContent).trim();
})
.sort(function(a,b){
returna.localeCompare(b)//按照順序排序
})
.map(function(txt){//組合html
return'<a>'+txt+'</a>';
})
.join('');

wrap.innerHtml=html;//輸出

Ⅵ JS實現首字母排序

一個按照姓名首字母排序的功能,支持數字,字母,符號,中文混合排序

Ⅶ js怎麼用sort把數組裡面的英文進行首字母排序,如圖,搞不懂這一行代碼,小白,求大佬。

harCodeAt(0) 方法返回字元串第一個字元的 Unicode 編碼。
sort()方法傳入的是一個函數,函內數有兩個參數容(pre,next)(參數名可以任意),依次傳入數組中的兩個元素,如果返回值小於0,則排序後pre排在next前面,

Ⅷ javascript中文按照拼音首字母排序,如何實現

中文拼音排序一直都是很有趣的一個問題。推薦使用這個函數 String.prototype.localeCompare(),鏈接 MDN。首先用 localeCompare 試下拼音排序

var array = ['武漢', '北京', '上海', '天津'];

array.sort(

function compareFunction(param1, param2) {

return param1.localeCompare(param2);

}

);

array // ["北京", "上海", "天津", "武漢"]

然後根據 26 個英文字母分組排序,函數如下,

function pySegSort(arr) {

if(!String.prototype.localeCompare)

return null;

var letters = "*abcdefghjklmnopqrstwxyz".split('');

var zh = "阿八嚓噠妸發旮哈譏咔垃痳拏噢妑七呥扨它穵夕丫帀".split('');

var segs = [];

var curr;

letters.forEach(function(item,i){

curr = {letter: item, data:[]};

arr.forEach(function(item2){

if((!zh[i-1] || zh[i-1].localeCompare(item2) <= 0) && item2.localeCompare(zh[i]) == -1) {

curr.data.push(item2);

}

});

if(curr.data.length) {

segs.push(curr);

curr.data.sort(function(a,b){

return a.localeCompare(b);

});

}

});

return segs;

}

測試:

pySegSort(["我","不","懂","愛","啊","按","已","呀","選","縣"])

結果:

[{"letter":"a","data":["啊","愛","按"]},{"letter":"b","data":["不"]},{"letter":"d","data":["懂"]},{"letter":"w","data":["我"]},{"letter":"x","data":["縣","選"]},{"letter":"y","data":["呀","已"]}]

Ⅸ js 怎麼把字元串組 按順序a-z,A-Z,0-9,的順序排列

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN""
">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gbk">
<title>UntitledDocument</title>
<script>
window.onload=function(){
vara="1我來a2你b3他d";//要排序的字元串a
varb=a.split("");//分割源字元串a為數組b
b.sort();//數組b升序排序(系統自帶的方法)
varc=b.join("");//把數組b每個元素連接成字元串c
alert(c);//輸出最終排序好的字元串
}
</script>
</head>
<body>
</body>
</html>

整個過程注釋寫得很清楚了,希望對你有幫助

Ⅹ 有10個字母,如何用JS把A、B、C、D、E、F、G、H、I、J、 這10個字母隨機組成5個為一組的兩組排序。

首先隨機數會把
然後一個for循環
每次循環的時候隨機一個數
當這個隨機數大於0.5的時候將當前字母放入a數組
當這個隨機數小與0.5的時候將當前字母放人b數組
//升級
先將上面10個字母編號
然後然後生成隨機數將上面的字母隨機排序(出現過的重新隨機)
然後再執行上面的for循環

閱讀全文

與js實現字母排序demo相關的資料

熱點內容
日常型常量數據用什麼符號引起 瀏覽:198
黑人英語課作弊播放 瀏覽:602
有劇情的露點電影 瀏覽:799
家旺家俊阿梅是哪個電影 瀏覽:318
皮皮陪練app怎麼發布動態 瀏覽:565
什麼app可以批量添加自定義水印 瀏覽:885
sd目錄在哪個文件夾里 瀏覽:558
台灣抵抗日本侵略的電影 瀏覽:461
c盤太大如何確定是哪個文件 瀏覽:656
windows64位網路編程 瀏覽:517
他經常會去看電影的英語 瀏覽:714
大學英語補習班用哪個app 瀏覽:979
考勤登記管理系統java 瀏覽:816
蘋果在線免費下載觀看 瀏覽:230
pgyy.vlp 瀏覽:819
免費可以看污的 瀏覽:539
主人公是勞拉的電影 瀏覽:354
java漢字轉utf8 瀏覽:310
dnf90版本赫拉斯 瀏覽:937
在電腦上怎麼保存文件 瀏覽:233

友情鏈接