2011-03-18 109 views
0

我有一個包含文本和html的數組。 myArray的[1]包含以下.. myArray的[2]包含同樣的事情,只是不同的名字「約翰」等我如何根據其中的一個內容對數組進行排序

153); color: white; cursor: pointer; font-size: 12px; font-weight: normal; font-family: 
Arial;" onmouseover="this.style.backgroundColor = 'white'; this.style.color='#336699'" 
onmouseout="this.style.backgroundColor = ''; this.style.color = 'white'; " 
onclick="if(top.mail.checkDataset()) mail.execFunction('javascript: changeMain(\'orderCreate1.do\')')"> 

<img alt="" src="images/round-white-bullet.gif" height="10" width="10"> John<br> 

整個陣列,使其忽略一切,由名sortsd我怎樣才能進行排序'約翰'在這種情況下?名稱總是在圖像之後和之前br如果有幫助

+2

請讓您的文章易讀。 – Mat 2011-03-18 11:58:01

+0

你的意思是單個數組元素具有所有標記嗎?在你的例子中,不清楚在這裏分隔數組元素。目前還不清楚「按照約翰'的名稱排序」的含義。你能澄清一下嗎? – kojiro 2011-03-18 11:59:44

+0

OP中給出的代碼與該問題不符,請編輯 – 3rgo 2011-03-18 12:53:24

回答

1

問題陳述有點含糊,但一般來說,您可以使用某些函數對數組進行排序,該函數提取要排序的鍵(我將通過使用類似下面的調用這個函數key這裏):

array.sort(function(x, y) { 
    var x_key = key(x); 
    var y_key = key(y); 
    return (x_key < y_key ? -1 : x_key === y_key ? 0 : 1); 
}); 

在你的情況,key將不得不從一個數組元素提取名稱的函數。

如果你的數組元素都是字符串,你可能會想嘗試的正則表達式:

var key = function(x) { 
    return /<img[^>]*>([^<]*)<br>/.exec(x)[1]; 
}; 
+0

如何繼續實現關鍵功能 – code511788465541441 2011-03-18 12:50:00

相關問題